How regular expressions works in JavaScript(regex)

How regular expressions works in JavaScript(regex)

What are Regular Expressions or regex in JavaScript?

Regular expressions are patterns used to match character combinations in strings.

What do Regular Expressions allow you to do?

Regular expressions are used with the regex methods test() and exec() and it allow you to do match() , replace() , search() , and split() .

Syntax of regex in JS

/pattern/flags

Flags in Regular Expressions

A regular expression in JS consists of 6 flags: i, g, m, u, s, y. Without flags, the search by a regex is the same as a substring search.

  • i:- Ignore Casing mode ( the search is case-insensitive: no difference between A and a )

  • g:- global mode (Makes the expression search for all occurrences)

  • m:- Multiline mode (Makes the boundary characters ^ and $ match the beginning and end of every single line instead of the beginning and end of the whole string.)

  • s:- Dot All mode (Makes a wild character . matches newlines as well.)

  • u:- Unicode mode (Makes the expression assume individual characters as code points, not code units, and thus match 32-bit characters as well.)

  • y:- Sticky mode (searching at the exact position in the text)

How to add these flags to a regex?

forward slashes //, flags come after the second slash. In general notation we can express this as follows:

> /pattern/flags

Let’s look at the example below of Regular Expressions

1. It means to match one e or more than one e.

> /e+/g

image.png

2. It means to match one e or more than one e and follow-up a and a is optional.

> /e+a?/g

image.png

3. * is a combination of? and +, So it works the same as above.

> /ea*/g

image.png

4. It means to match any letter to the sentence.

> /\w/g

image.png

5. It means to match any form of white space.

> /\s/g

image.png

6. It means to match anything that is not a white space.

> /\S/g

image.png

7. It means to match anything that is not a letter in a sentence.

> /\W/g

image.png

8. It means to match any set of characters whose length is between 6 and 7.

> /\w{6,7}/g

image.png

9. It means to match any character specified in the bracket[].

> /[shubham]/g

image.png