RegExp constructor creates a regular expression object for matching text with a pattern.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Literal, constructor, and factory notations are possible:
/pattern/flags new RegExp(pattern[, flags]) RegExp(pattern[, flags])
- 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.
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 (and
lastIndexwill be reset to
0) (as of ES2015).
flagsis not specified and a regular expressions object is supplied, that object's flags (and
lastIndexvalue) will be copied over.
flagsmay contain any combination of the following characters:
- Find all matches rather than stopping after the first match.
uflag is also enabled, use Unicode case folding.
- Treat beginning and end characters (
$) as working over multiple lines. In other words, match the beginning or end of each line (delimited by
\r), not only the very beginning or end of the whole input string.
.to match newlines.
patternas a sequence of Unicode code points. (See also Binary strings).
- 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.
|ECMAScript Latest Draft (ECMA-262)
The definition of 'RegExp constructor' in that specification.
|Chrome Full support 1||Edge Full support 12||Firefox Full support 1||IE Full support 4||Opera Full support Yes||Safari Full support 1||WebView Android Full support 1||Chrome Android Full support 18||Firefox Android Full support 4||Opera Android Full support Yes||Safari iOS Full support 1||Samsung Internet Android Full support 1.0||nodejs Full support Yes|
- Full support
- Full support