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.


Literal, constructor, and factory notations are possible:

new RegExp(pattern[, flags])
RegExp(pattern[, flags])


The text of the regular expression.
As of ES5, this can also be another RegExp object 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. 

If specified, flags is a string that contains the flags to add.

Alternatively, if an object is supplied for the pattern, the flags string will replace any of that object's flags (and lastIndex will be reset to 0) (as of ES2015).

If flags is not specified and a regular expressions object is supplied, that object's flags (and lastIndex value) will be copied over.

flags may contain any combination of the following characters:

g (global match)
Find all matches rather than stopping after the first match.
i (ignore case)
If u flag 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 \n or \r), not only the very beginning or end of the whole input string.
s ("dotAll")
Allows . to match newlines.
u (unicode)
Treat pattern as a sequence of Unicode code points. (See also Binary strings).
y (sticky)
Matches only from the index indicated by the lastIndex property 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.

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
RegExp() constructorChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support YesSafari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support Yes


Full support  
Full support

See also