Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The RegExp() constructor creates RegExp objects.
For an introduction to regular expressions, read the Regular Expressions chapter in the JavaScript Guide.
Note: RegExp() can be called with or without new, but sometimes with different effects. See Return value.
The text of the regular expression. This can also be another RegExp object.
flags OptionalIf specified, flags is a string that contains the flags to add. Alternatively, if a RegExp object is supplied for the pattern, the flags string will replace any of that object's flags (and lastIndex will be reset to 0).
flags may contain any combination of the following characters:
d (indices)Generate indices for substring matches.
g (global)Find all matches rather than stopping after the first match.
i (ignore case)When matching, casing differences are ignored.
m (multiline)Treat beginning and end assertions (^ 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.
v (unicodeSets)An upgrade to the u flag that enables set notation in character classes as well as properties of 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.
RegExp(pattern) returns pattern directly if all of the following are true:
In all other cases, calling RegExp() with or without new both create a new RegExp object. If pattern is a regex, the new object's source is pattern.source; otherwise, its source is pattern coerced to a string. If the flags parameter is not undefined, the new object's flags is the parameter's value; otherwise, its flags is pattern.flags (if pattern is a regex).
Thrown in one of the following cases:
There are two ways to create a RegExp object: a literal notation and a constructor.
The following three expressions create the same regular expression:
Before regular expressions can be used, they have to be compiled. This process allows them to perform matches more efficiently. There are two ways to compile and get a RegExp object.
The literal notation results in compilation of the regular expression when the expression is evaluated. On the other hand, the constructor of the RegExp object, new RegExp('ab+c'), results in runtime compilation of the regular expression.
Use a string as the first argument to the RegExp() constructor when you want to build the regular expression from dynamic input.
| ECMAScript® 2027 Language Specification # sec-regexp-constructor |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 10, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.