RegExp

Files

Creates a regular expression object for matching text according to a pattern.

When using the constructor function, the normal string escape rules (preceding
special characters with \ when included in a string) are necessary. For
example, the following are equivalent:

var re = new RegExp("\w+");
var re = /\w+/;

Notice that the parameters to the literal format do not use quotation marks to
indicate strings, while the parameters to the constructor function do use
quotation marks. So the following expressions create the same regular
expression:

/ab+c/i;
new RegExp("ab+c", "i");

Special characters in regular expressions

Character

Meaning

\

For characters that are usually treated literally, indicates that the next character

is special and not to be interpreted literally.

For example, /b/ matches the character 'b'. By placing a backslash in front of b, that

is by using /\b/, the character becomes special to mean match a word boundary.

or

For characters that are usually treated specially, indicates that the next character is

not special and should be interpreted literally.

For example, * is a special character that means 0 or more occurrences of the preceding

character should be matched; for example, /a*\/ means match 0 or more "a"s. To match *

literally, precede it with a backslash; for example, /a\*\/ matches 'a*'.

^

Matches beginning of input. If the multiline flag is set to true, also matches

immediately after a line break character.

For example, /^A/ does not match the 'A' in "an A", but does match the first 'A' in

"An A".

$

Matches end of input. If the multiline flag is set to true, also matches immediately

before a line break character.

For example, /t$/ does not match the 't' in "eater", but does match it in "eat".

Matches the character with the Unicode value hhhh (four hexadecimal digits)

The literal notation provides compilation of the regular expression when the expression is evaluated. Use
literal notation when the regular expression will remain constant. For example, if you use literal notation
to construct a regular expression used in a loop, the regular expression won't be recompiled on each iteration.

The constructor of the regular expression object, for example, new RegExp("ab+c"), provides runtime
compilation of the regular expression. Use the constructor function when you know the regular expression
pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.

The value of multiline is true if the "m" flag was used; otherwise, false. The "m" flag
indicates that a multiline input string should be treated as multiple lines. For example, if "m"
is used, "^" and "$" change from matching at only the start or end of the entire string to the
start or end of any line within the string.

Parameters

"m" - Treat beginning and end characters (^ and $) as working over multiple lines
(i.e., match the beginning or end of each line (delimited by \n or \r), not
only the very beginning or end of the whole input string)

Returns

If the match succeeds, the exec method returns an array and updates properties of the regular
expression object. The returned array has the matched text as the first item, and then one item for
each capturing parenthesis that matched containing the text that was captured. If the match fails,
the exec method returns null.

If you are executing a match simply to find true or false, use the test method or the String
search method.

Consider the following example:

// Match one d followed by one or more b's followed by one d
// Remember matched b's and the following d
// Ignore case
var re = /d(b+)(d)/ig;
var result = re.exec("cdbBdbsbz");

The following table shows the results for this script:

Object

Property/Index

Description

Example

result

The content of myArray.

["dbBd", "bB", "d"]

index

The 0-based index of the match in the string

1

input

The original string.

cdbDdbsbz

[0]

The last matched characters.

dbBd

[1], ...[n]

The parenthesized substring matches, if any. The number of possible

[1] = bB

parenthesized substrings is unlimited.

[2] = d

re

lastIndex

The index at which to start the next match.

5

ignoreCase

Indicates the "i" flag was used to ignore case.

true

global

Indicates the "g" flag was used for a global match.

true

multiline

Indicates the "m" flag was used to search in strings across

false

multiple lines.

source

The text of the pattern.

d(b+)(d)

If your regular expression uses the "g" flag, you can use the exec method multiple times to find
successive matches in the same string. When you do so, the search starts at the substring of str
specified by the regular expression's lastIndex property (test will also advance the lastIndex
property). For example, assume you have this script:

Returns

When you want to know whether a pattern is found in a string use the test method (similar to the
String.search method); for more information (but slower execution) use the exec method (similar to
the String.match method). As with exec (or in combination with it), test called multiple times on
the same global regular expression instance will advance past the previous match.

The following example prints a message which depends on the success of the test:

The RegExp object overrides the toString method of the Object object; it does not inherit
Object.toString. For RegExp objects, the toString method returns a string representation of the
regular expression.