JavaScript RegExp(Regular Expression)

A regular expression is a character sequence defining a search pattern. It’s employed in text searches and replacements, describing what to search for within a text. Ranging from single characters to complex patterns, regular expressions enable various text operations with versatility and precision.

A regular expression can be a single character or a more complicated pattern.

Syntax:

/pattern/modifiers;

Example:

let patt = /w3wiki/i;

Explanation : 

/w3wiki/i is a regular expression.
w3wiki is the pattern (to be used in a search).
i is a modifier (modifies the search to be Case-Insensitive).

Regular Expression Modifiers can be used to perform multiline searches which can also be set to case-insensitive matching: 

ExpressionsDescriptions
gFind the character globally
iFind a character with case-insensitive matching
mFind multiline matching

Regular Expression Brackets can Find characters in a specified range

ExpressionsDescription
[abc]Find any of the characters inside the brackets
[^abc]Find any character, not inside the brackets
[0-9]Find any of the digits between the brackets 0 to 9
[^0-9]Find any digit not in between the brackets
(x | y)Find any of the alternatives between x or y separated with |

Regular Expression Metacharacters are characters with a special meaning:

MetacharacterDescription
\.Search single characters, except line terminator or newline.
\wFind the word character i.e. characters from a to z, A to Z, 0 to 9
\dFind a digit
\DSearch non-digit characters i.e all the characters except digits
\sFind a whitespace character
\SFind the non-whitespace characters.
\bFind a match at the beginning or at the end of a word
\BFind a match that is not present at the beginning or end of a word.
\0Find the NULL character.
\nFind the newline character.
\fFind the form feed character
\rFind the carriage return character
\tFind the tab character
\vFind the vertical tab character
\uxxxxFind the Unicode character specified by the hexadecimal number xxxxx

Regular Expression Quantifiers are used to define quantitiesoccurrence

QuantifierDescription
n+Match any string that contains at least one n
n*Match any string that contains zero or more occurrences of n
n?Match any string that contains zero or one occurrence of n
m{X}Find the match of any string that contains a sequence of m, X times
m{X, Y}Find the match of any string that contains a sequence of m, X to Y times
m{X,}Find the match of any string that contains a sequence of m, at least X times
m$Find the match of any string which contains m at the end of it
^mFind the match of any string which contains m at the beginning of it
?!mFind the match of any string which is not followed by a specific string m.

Regular Expression Object Properties:

PropertyDescription
constructorReturn the function that created the RegExp object’s prototype
globalSpecify whether the “g” modifier is set or not
ignorecaseSpecify whether the “i” modifier is set or not
lastindexSpecify the index at which to start the next match
multilineSpecify whether the “m” modifier is set or not
sourceReturn the text of RegExp pattern

Regular Expression Object Methods:

MethodDescription
compile()Used to compile the regular expression while executing of script
exec()Used to test for the match in a string.
test()Used to test for a match in a string
toString()Return the string value of the regular expression

Below is an example of the JavaScript Regular Expressions. 

Example:  

JAVASCRIPT
function GFGFun() {
    let str = "Visit w3wiki";
    let n = str.search(/w3wiki/i);
    console.log(n);
}
GFGFun();

Output:  

6

Using String Methods

In JavaScript, regular expressions are often used with the two string methods: search() and replace().

  • The search() method uses an expression to search for a match and returns the position of the match.
  • The replace() method returns a modified string where the pattern is replaced.

Using String search() With a Regular Expression

Use a regular expression to do a case-insensitive search for “w3wiki” in a string:

Example:

JAVASCRIPT
function myFunction() {

    // input string
    let str = "Visit w3wiki!";

    // searching string with modifier i
    let n = str.search(/w3wiki/i);

    console.log(n);

    // searching string without modifier i
    let n = str.search(/w3wiki/);

    console.log(n);
}
myFunction();

Output: 

6
-1

Use String replace() With a Regular Expression

Use a case-insensitive regular expression to replace gfG with w3wiki in a string:

Example: 

JAVASCRIPT
function myFunction() {

    // input string
    let str = "Please visit gfG!";

    // replacing with modifier i
    let txt = str.replace(/gfg/i, "w3wiki");
    
    console.log(txt);
}
myFunction();

Output
Please visit w3wiki!