In JavaScript, regular expressions are commonly used with two string methods: search() and replace().
The search() method uses a regular expression to find a match and returns the position of the first match.
The replace() method returns a new string with the specified pattern replaced.
The search() method searches a string for a specified value and returns the index of the first match.
Use a string to search for code7school within another string.
let text = “Visit code7school!”; |
The value stored in n
will be:
6 |
Use a regular expression to perform a case-insensitive search for “code7school” within a string.
let text = “Visit code7school”; |
The value stored in n
will be:
6 |
The replace() method replaces a specified value in a string with a different value.
let text = “Visit Microsoft!”;
|
Use a case-insensitive regular expression to replace “Microsoft” with “code7school” in a string.
let text = “Visit Microsoft!”; |
The value stored in res
will be:
Visit code7school! |
Modifiers can be used to conduct case-insensitive or global searches.
Modifier |
Description |
i |
Perform case-insensitive matching |
g |
Perform a global match (find all) |
m |
Perform multiline matching |
d |
Perform start and end matching (New in ES2022) |
Brackets are used to specify a range of characters to match.
Expression |
Description |
[abc] |
Find any of the characters between the brackets |
[0-9] |
Find any of the digits between the brackets |
(x|y) |
Find any of the alternatives separated with | |
Metacharacters are characters that have a special significance or function in regular expressions.
Metacharacter |
Description |
\d |
Find a digit |
\s |
Find a whitespace character |
\b |
Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b |
\uxxxx |
Find the Unicode character specified by the hexadecimal number xxxx |
Quantifiers define quantities:
Quantifier |
Description |
n+ |
Matches any string that contains at least one n |
n* |
Matches any string that contains zero or more occurrences of n |
n? |
Matches any string that contains zero or one occurrences of n |
In JavaScript, the RegExp object is a regular expression that comes with predefined properties and methods.
The test() method is a RegExp method that searches a string for a pattern and returns true or false based on whether a match is found.
The following example searches for the character “e” in a string:
const pattern = /e/; pattern.test(“The best things in life are free!”); |
Since the string contains the character “e,” the output of the code above will be:
true |
You don’t need to store the regular expression in a variable first. The two lines above can be condensed into a single line.
/e/.test(“The best things in life are free!”); |
The exec() method is a RegExp method that searches a string for a specified pattern and returns the matched text as an object.
If no match is found, it returns null.
The following example searches for the character “e” in a string:
/e/.exec(“The best things in life are free!”); |