JavaScript Regular Expressions


Regular expressions (Regular Expression, often abbreviated as regex, regexp, or RE in the code) use a single string to describe and match a series of string search patterns that conform to a certain syntax rule.

The search mode can be used for text search and text replacement.


What is a Regular Expression?

Regular expressions are search patterns formed by a sequence of characters.

When you search for data in text, you can use search patterns to describe what you are looking for.

The regular expression can be a simple character, or a more complex pattern.

Regular expressions can be used for all text search and text replacement operations.

Grammar

/Regular Expression Body/ Modifier (optional)

The modifier is optional.

Example

var patt = /tutorialfish/i

Example analysis:

/tutorialfish/i    is a regular expression.

tutorialfish   is a regular expression body (used for retrieval).

i   is a modifier (search is not case sensitive).


Use String Methods

In JavaScript, regular expressions are commonly used in two string methods: search() and replace().

The search() method is used to retrieve the specified substring in the string, or retrieve the substring that matches the regular expression, and return the starting position of the substring.

The replace() method is used to replace some characters with other characters in a string, or replace a substring that matches a regular expression.


The search() Method Uses Regular Expressions

Example

Use regular expressions to search for the "TutorialFish" string, and it is not case sensitive:

var str = "Visit TutorialFish!";
var n = str.search(/TutorialFish/i);

The output is:

6

Try It! 


search() Method Uses String

The search method can use a string as a parameter. String parameters are converted to regular expressions:

Example

Retrieve the substring of "TutorialFish" in the string:

var str = "Visit TutorialFish!";
var n = str.search("TutorialFish");

 Try It! 


replace() Method Uses Regular Expressions

Example

Use regular expressions to replace Microsoft with TutorialFish in the string without being case-sensitive:

var str = document.getElementById("demo").innerHTML;
var txt = str.replace(/microsoft/i,"TutorialFish");

The resulting output is:

Visit TutorialFish!

 Try It! 

replace() Method Uses String

The replace() method will receive a string as a parameter:

var str = document.getElementById("demo").innerHTML;
var txt = str.replace("Microsoft","TutorialFish");

 Try It! 


Did You Notice?

NoteRegular expression parameters can be used in the above methods (instead of string parameters).
Regular expressions make the search function more powerful (such as case insensitive in the examples).

Regular Expression Modifiers

Modifiers can be case-insensitive in global searches:

ModifierDescription
iPerform case-insensitive matching.
gPerform a global match (find all matches instead of stopping after the first match is found).
mPerform multi-line matching.



Regular Expression Pattern

Square brackets are used to find characters in a certain range:

ExpressionDescription
[abc]Find any characters between the square brackets.
[0-9]Find any number from 0 to 9.
(x | y)Find any options separated by | .

Metacharacters are characters with special meaning:

MetacharacterDescription
\dFind the number.
\sFind blank characters.
\bMatch word boundaries.
\ uxxxxFind the Unicode character specified by the hexadecimal number xxxx.

Quantifier:

QuantifierDescription
n+Matches any string containing at least one n .
n*Matches any string containing zero or more n .
n?Matches any string containing zero or one n .



Use RegExp Object

In JavaScript, the RegExp object is a regular expression object with predefined properties and methods.


Use test()

The test() method is a regular expression method.

The test() method is used to detect whether a string matches a certain pattern. If the string contains matching text, it returns true, otherwise it returns false.

The following example is used to search for the character "e" in a string:

Example

var patt = /e/;
patt.test("The best things in life are free!");

The string contains "e", so the output of this example is:

true

 Test It! 

You don't need to set the regular expression variable, the above two lines of code can be combined into one line:

/e/.test("The best things in life are free!")



Use exec()

The exec() method is a regular expression method.

The exec() method is used to retrieve the match of the regular expression in the string.

This function returns an array in which the matching results are stored. If no match is found, the return value is null.

The following example is used to search for the letter "e" in a string:

Example

/e/.test("The best things in life are free!")

The string contains "e", so the output of this example is:

e

 Try It! 


More Examples


Complete RegExp Reference Manual

For the complete RegExp object references , please refer to our JavaScript RegExp references .

This reference contains all the methods and properties of the RegExp object.