Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Regular Expressions

JavaScript Regular Expressions


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

A search pattern 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 single character or a more complex pattern.

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

For more on regular expressions, visit:Regular Expression - Tutorial.

Syntax



/正则表达式主体/修饰符(可选)

The modifiers are optional.

Example:

var patt = /runoob/i

Example analysis:

/runoob/i  is a regular expression.

runoob  is aRegular expression body (used for retrieval).

i  is aModifiers (case-insensitive search).


Using string methods

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

The **search() ** method is used to retrieve a specified substring from a string, or a substring matching a regular expression, and returns the starting position of the substring.

The **replace() ** method is used to replace some strings with others in a string, or to replace a substring matching a regular expression.


The search() method with a regular expression

Example

Use a regular expression to search for the string 'Runoob', case-insensitively:

var

str

=

"

Visit Runoob!

"

;

var

n

=

str

.

search

(

/

Runoob

/

i

)

;

The output is:

6

Try it »


The search() method with a string

The search method can take a string as an argument. A string argument is converted to a regular expression:

Example

Retrieve the substring 'Runoob' from the string:

var

str

=

"

Visit Runoob!

"

;

var

n

=

str

.

search

(

"

Runoob

"

)

;

Try it »


The replace() method with a regular expression

Example

Use a case-insensitive regular expression to replace Microsoft with Runoob in a string:

var

str

=

document

.

getElementById

(

"

demo

"

)

.

innerHTML

;

var

txt

=

str

.

replace

(

/

microsoft

/

i

,

"

Runoob

"

)

;

The output result is:

Visit Runoob!

Try it »

The replace() method with a string

The replace() method accepts a string as an argument:

var

str

=

document

.

getElementById

(

"

demo

"

)

.

innerHTML

;

var

txt

=

str

.

replace

(

"

Microsoft

"

,

"

Runoob

"

)

;

Try it »


Did you notice?

| Note | A regular expression argument can be used in the methods above (instead of a string argument).
Regular expressions make searching more powerful (such as case-insensitivity in the example). | | --- | --- |


Regular expression modifiers

Modifiers Can be case-insensitive in a global search:

Modifiers Description
i Performs a case-insensitive match.
g Performs a global match (finds all matches rather than stopping after the first match).
m Performs a multiline match.

Regular expression patterns

Brackets are used to find a character within a certain range:

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

Metacharacters are characters with special meanings:

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

Quantifiers:

Quantifier Description
n+ Matches any string containing at least one n the specified string.
n* Matches any string containing zero or more n the specified string.
n? Matches any string containing zero or one n the specified string.

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 checks whether a string matches a pattern; if the string contains matching text, it returns true, otherwise it returns false.

The following example searches 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 example outputs:

true

Try it »

You don't need to set a variable for the regular expression; the above two lines can be combined into one:

/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 a match of a regular expression in a string.

This function returns an array containing the match results. If no match is found, the return value is null.

The following example searches for the letter 'e' in a string:

Example 1

/e/.exec(

"The best things in life are free!"

);

The string contains 'e', so the example outputs:

e

Try it »


More examples


Complete RegExp reference manual

For the complete RegExp object reference manual, please refer to our JavaScript RegExp Reference Manual。

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