JavaScript Regular Expressions


A regular expression (English: Regular Expression, often abbreviated as regex, regexp, or RE in code) uses a single string to describe and match a series of string search patterns that conform to a certain syntactic rule.

Search patterns can be used for text search and text replacement.


What is a Regular Expression?

A regular expression is a search pattern formed by a sequence of characters.

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

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

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

For more regular expression content, visit:Regular Expressions - Tutorial.

Syntax

/regex body/modifiers (optional)

The modifiers are optional.


Example:

var patt = /example/i

Example analysis:

/example/iis a regular expression.

exampleis aregular expression body(used for searching).

iis amodifier(search is case-insensitive).


Using String Methods

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

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

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


search() method using a regular expression

Example

Use a regular expression to search for the string "Example" without distinguishing case:

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

The output result is:

6

Try it Yourself »


search() method using a string

The search method can use a string as an argument. The string argument will be converted to a regular expression:

Example

Search for the substring "Example" in the string:

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

Try it Yourself »


replace() method using a regular expression

Example

Use a regular expression without distinguishing case to replace Microsoft with Example in the string:

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

The result output is:

Visit Example!

Try it Yourself »

replace() method using a string

The replace() method will accept a string as an argument:

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

Try it Yourself »


Did you notice?


Note Regular expression arguments can be used in the above methods (instead of string arguments).
Regular expressions make search functionality more powerful (e.g., case-insensitive search in the example).


Regular Expression Modifiers

ModifiersCan be used for case-insensitive global searches:

Modifier Description
i Perform a case-insensitive match.
g Perform a global match (find all matches rather than stopping after the first match).
m Perform a multiline match.


Regular Expression Patterns

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

Expression Description
[abc] Find any character between the square brackets.
[0-9] Find any digit from 0 to 9.
(x|y) Find any of the options separated by |.

Metacharacters are characters that have special meanings:

Metacharacter Description
\d Find a digit.
\s Find a whitespace character.
\b Match a word boundary.
\uxxxx Find a Unicode character specified by the hexadecimal number xxxx.

Quantifiers:

Quantifier Description
n+ Matches any string containing at least onen.
n* Matches any string containing zero or moren.
n? Matches any string containing zero or onen.


Using the RegExp Object

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


Using test()

The test() method is a regular expression method.

The test() method is used to test whether a string matches a 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

Try it Yourself »

You can avoid setting a regular expression variable; the above two lines of code can be combined into one line:

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


Using exec()

The exec() method is a regular expression method.

The exec() method is used to search for matches of a regular expression in a string.

This function returns an array containing the matching results. 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 1

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

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

e

Try it Yourself »

More Examples

  • JS to determine whether the input string is composed of numbers, letters, and underscores

  • JS to determine whether the input string is all letters

  • JS to determine whether the input string is all numbers


Complete RegExp Reference Manual

For the complete RegExp object reference, please refer to ourJavaScript RegExp Reference Manual。

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

Other Extensions