RegExp
The RegExp global object represents a regular expression, a pattern that describes a set of strings and can search, test, and extract text that matches it. Regular expression literals such as /\d+/g create RegExp objects too, and String methods like .replace() and .split() accept them as patterns:
> /\d{4}-\d{2}/.test("Due 2026-11")
true
> new RegExp("ab+c", "gi")
/ab+c/gi
RegExp.escape() was added in ES2025 and is Baseline Newly available since 2025. Pattern modifiers such as (?i:...) were added in ES2025 and are also Baseline Newly available since 2025.
Syntax
A RegExp comes from a literal or from the constructor, which takes a pattern and optional flags:
/<pattern>/[<flags>]
new RegExp(<pattern>[, <flags>])
RegExp(<pattern>[, <flags>])
| Parameter | Description |
|---|---|
pattern |
The text of the regular expression as a string, or an existing RegExp to copy |
flags |
An optional string of flags. Each flag can appear once, in any order |
The new keyword is optional, and both forms return a RegExp object. Without new, passing a RegExp and no flags returns that same object instead of a copy. The available flags are:
| Flag | Property | Effect |
|---|---|---|
d |
.hasIndices |
Adds start and end positions of each capture to the match result (ES2022) |
g |
.global |
Finds every match instead of stopping at the first |
i |
.ignoreCase |
Ignores letter case |
m |
.multiline |
Makes ^ and $ match at line breaks |
s |
.dotAll |
Lets . match line terminators |
u |
.unicode |
Treats the pattern as a sequence of Unicode code points |
v |
.unicodeSets |
Upgrades u mode with set operations in character classes (ES2024) |
y |
.sticky |
Matches only at the position in .lastIndex |
The u and v flags can’t be combined, and invalid flags or patterns throw a SyntaxError.
To see how the g, i, m, s, and y flags change what a pattern matches, and how repeated .test() calls move .lastIndex, edit the pattern and toggle the flags below:
Key Static Methods and Properties
| Member | Kind | Description |
|---|---|---|
RegExp.escape() |
Static method | Returns a copy of a string with regex syntax characters, other punctuation, and whitespace escaped, so it matches literally (ES2025) |
RegExp.$1, RegExp.lastMatch |
Static property | Holds parts of the most recent match from any regex, kept only for legacy code |
Key Instance Methods and Properties
| Member | Kind | Description |
|---|---|---|
.test() |
Instance method | Returns true when the string contains a match |
.exec() |
Instance method | Returns a match array with captures and .groups, or null |
.source |
Instance property | Returns the pattern text without slashes or flags |
.flags |
Instance property | Returns the flags as a string in a fixed order |
.lastIndex |
Instance property | Holds the index where the next search starts for g and y regexes |
.toString() |
Instance method | Returns the regex in literal form, such as "/ab+c/gi" |
The string methods .match(), .matchAll(), .replace(), .replaceAll(), .search(), and .split() call the regex’s symbol-keyed methods, such as [Symbol.replace](), when they receive a RegExp.
Examples
A log parser pulls the date out of each line. Named capture groups label the parts, and .exec() returns them in .groups:
> const datePattern = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
undefined
> datePattern.exec("Shipped on 2026-11-05").groups
[Object: null prototype] { year: '2026', month: '11', day: '05' }
A receipt scanner needs every amount in a line of text. With the g flag, .matchAll() yields one match per amount:
> const receipt = "Coffee 3.50, bagel 2.25, tip 1.00";
undefined
> [...receipt.matchAll(/\d+\.\d{2}/g)].map((match) => Number(match[0]))
[ 3.5, 2.25, 1 ]
A changelog search box looks up whatever the user types, including characters like . that have special meaning in patterns. RegExp.escape() makes the input match literally:
> const query = "v1.2";
undefined
> new RegExp(RegExp.escape(query)).test("Fixed in v1x2")
false
> new RegExp(query).test("Fixed in v1x2")
true
The unescaped pattern matches v1x2, because . matches any character there.
Common Gotchas
A regex with the g or y flag remembers where its last match ended in .lastIndex, so repeated .test() calls on the same string can alternate between true and false:
> const hasDigit = /\d/g;
undefined
> hasDigit.test("a1")
true
> hasDigit.test("a1")
false
Inside a string passed to the constructor, a backslash must be doubled. Otherwise, the string escape is processed first and the pattern loses its backslash:
> new RegExp("\d+").source
'd+'
> new RegExp("\\d+").source
'\\d+'
Related Resources
Course
JavaScript Regular Expressions and Text Formatting for Pythonistas
Port a payments team's release-notes script from Python to JavaScript, and learn to line up text in columns and to test, extract, and rewrite text with JavaScript's regular expressions.
For additional information on related topics, take a look at the following resources:
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 18, 2026