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.
* Some parts of this feature may have varying levels of support.
The RegExp object is used for matching text with a pattern.
For an introduction to regular expressions, read the Regular expressions chapter in the JavaScript guide. For detailed information of regular expression syntax, read the regular expression reference.
There are two ways to create a RegExp object: a literal notation and a constructor.
The following three expressions create the same regular expression object:
Before regular expressions can be used, they have to be compiled. This process allows them to perform matches more efficiently. More about the process can be found in dotnet docs.
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.
The expression new RegExp(/ab+c/, flags) will create a new RegExp using the source of the first parameter and the flags provided by the second.
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:
Note: Whether something is a "regex" can be duck-typed. It doesn't have to be a RegExp!
Some built-in methods would treat regexes specially. They decide whether x is a regex through multiple steps:
Note that in most cases, it would go through the Symbol.match check, which means:
This choice was made because [Symbol.match]() is the most indicative property that something is intended to be used for matching. (exec could also be used, but because it's not a symbol property, there would be too many false positives.) The places that treat regexes specially include:
For example, String.prototype.endsWith() would coerce all inputs to strings, but it would throw if the argument is a regex, because it's only designed to match strings, and using a regex is likely a developer mistake.
You can get around the check by setting [Symbol.match] to a falsy value that's not undefined. This would mean that the regex cannot be used for String.prototype.match() (since without [Symbol.match], match() would construct a new RegExp object with the two enclosing slashes added by re.toString()), but it can be used for virtually everything else.
Note that several of the RegExp properties have both long and short (Perl-like) names. Both names always refer to the same value. (Perl is the programming language from which JavaScript modeled its regular expressions.) See also deprecated RegExp properties.
Creates a new RegExp object.
Static read-only properties that contain parenthesized substring matches.
RegExp.input ($_)A static property that contains the last string against which a regular expression was successfully matched.
RegExp.lastMatch ($&)A static read-only property that contains the last matched substring.
RegExp.lastParen ($+)A static read-only property that contains the last parenthesized substring match.
RegExp.leftContext ($`)A static read-only property that contains the substring preceding the most recent match.
RegExp.rightContext ($')A static read-only property that contains the substring following the most recent match.
RegExp[Symbol.species]The constructor function that is used to create derived objects.
Escapes any potential regex syntax characters in a string, and returns a new string that can be safely used as a literal pattern for the RegExp() constructor.
These properties are defined on RegExp.prototype and shared by all RegExp instances.
RegExp.prototype.constructorThe constructor function that created the instance object. For RegExp instances, the initial value is the RegExp constructor.
RegExp.prototype.dotAllWhether . matches newlines or not.
RegExp.prototype.flagsA string that contains the flags of the RegExp object.
RegExp.prototype.globalWhether to test the regular expression against all possible matches in a string, or only against the first.
RegExp.prototype.hasIndicesWhether the regular expression result exposes the start and end indices of captured substrings.
RegExp.prototype.ignoreCaseWhether to ignore case while attempting a match in a string.
RegExp.prototype.multilineWhether or not to search in strings across multiple lines.
RegExp.prototype.sourceThe text of the pattern.
RegExp.prototype.stickyWhether or not the search is sticky.
RegExp.prototype.unicodeWhether or not Unicode features are enabled.
RegExp.prototype.unicodeSetsWhether or not the v flag, an upgrade to the u mode, is enabled.
These properties are own properties of each RegExp instance.
lastIndexThe index at which to start the next match.
(Re-)compiles a regular expression during execution of a script.
RegExp.prototype.exec()Executes a search for a match in its string parameter.
RegExp.prototype.test()Tests for a match in its string parameter.
RegExp.prototype.toString()Returns a string representing the specified object. Overrides the Object.prototype.toString() method.
RegExp.prototype[Symbol.match]()Performs match to given string and returns match result.
RegExp.prototype[Symbol.matchAll]()Returns all matches of the regular expression against a string.
RegExp.prototype[Symbol.replace]()Replaces matches in given string with new substring.
RegExp.prototype[Symbol.search]()Searches the match in given string and returns the index the pattern found in the string.
RegExp.prototype[Symbol.split]()Splits given string into an array by separating the string into substrings.
The following script uses the String.prototype.replace() method to match a name in the format first last and output it in the format last, first.
In the replacement text, the script uses $1 and $2 to indicate the results of the corresponding matching parentheses in the regular expression pattern.
This displays "Cruz, Maria".
The default line ending varies depending on the platform (Unix, Windows, etc.). The line splitting provided in this example works on all platforms.
Note that the order of the patterns in the regular expression matters.
By default, the . character does not match newlines. To make it match newlines, use the s flag (dotAll mode).
The sticky flag indicates that the regular expression performs sticky matching in the target string by attempting to match starting at RegExp.prototype.lastIndex.
With the sticky flag y, the next match has to happen at the lastIndex position, while with the global flag g, the match can happen at the lastIndex position or later:
With the global flag g, all 6 digits would be matched, not just 3.
\w and \W only matches ASCII based characters; for example, a to z, A to Z, 0 to 9, and _.
To match characters from other languages such as Cyrillic or Hebrew, use \uHHHH, where HHHH is the character's Unicode value in hexadecimal.
This example demonstrates how one can separate out Unicode characters from a word.
The Unicode property escapes feature provides a simpler way to target particular Unicode ranges, by allowing for statements like \p{scx=Cyrl} (to match any Cyrillic letter), or \p{L}/u (to match a letter from any language).
Note: Instead of using regular expressions for parsing URLs, it is usually better to use the browsers built-in URL parser by using the URL API.
| ECMAScript® 2027 Language Specification # sec-regexp-regular-expression-objects |
Enable JavaScript to view this browser compatibility table.
Starting with Firefox 34, in the case of a capturing group with quantifiers preventing its exercise, the matched text for a capturing group is now undefined instead of an empty string:
Note that due to web compatibility, RegExp.$N will still return an empty string instead of undefined (bug 1053944).
This page was last modified on Nov 9, 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.