RegExp() constructor
The RegExp constructor creates a regular expression
object for matching text with a pattern.
For an introduction to regular expressions, read the Regular Expressions chapter in the JavaScript Guide.
Syntax
Literal, constructor, and factory notations are possible:
/pattern/flags
new RegExp(pattern[, flags])
RegExp(pattern[, flags])
Parameters
pattern-
The text of the regular expression.
As of ES5, this can also be another
RegExpobject or literal (for the two RegExp constructor notations only). Patterns may include special characters to match a wider range of values than would a literal string. flags-
If specified,
flagsis a string that contains the flags to add.Alternatively, if an object is supplied for the pattern, the
flagsstring will replace any of that object's flags (andlastIndexwill be reset to0) (as of ES2015).If
flagsis not specified and a regular expressions object is supplied, that object's flags (andlastIndexvalue) will be copied over.flagsmay contain any combination of the following characters:d(indices)-
Generate indices for substring matches.
g(global match)-
Find all matches rather than stopping after the first match.
i(ignore case)-
If
uflag is also enabled, use Unicode case folding. m(multiline)-
Treat beginning and end characters (
^and$) as working over multiple lines. In other words, match the beginning or end of each line (delimited by\nor\r), not only the very beginning or end of the whole input string. s("dotAll")-
Allows
.to match newlines. u(unicode)-
Treat
patternas a sequence of Unicode code points. (See also Binary strings). y(sticky)-
Matches only from the index indicated by the
lastIndexproperty of this regular expression in the target string. Does not attempt to match from any later indexes.
Exceptions
- If
patterncannot be parsed as a valid regular expression, aSyntaxErroris thrown. -
If
flagscontains repeated characters or any character outside of those allowed, aSyntaxErroris thrown.
Examples
Literal notation and constructor
There are two ways to create a RegExp object: a literal notation
and a constructor.
- The literal notation's parameters are enclosed between slashes and do not use quotation marks.
- The constructor function's parameters are not enclosed between slashes but do use quotation marks.
The following three expressions create the same regular expression:
/ab+c/i
new RegExp(/ab+c/, 'i') // literal notation
new RegExp('ab+c', 'i') // constructor
The literal notation results in 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')—results in 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.
Specifications
| Specification |
|---|
| ECMAScript Language Specification # sec-regexp-constructor |
Browser compatibility
BCD tables only load in the browser