A regular expression is a pattern written as a literal,
/pattern/flags, or built with new RegExp(pattern, flags)
(ES3; ECMA-262, RegExp (Regular Expression) Objects). The literal’s surrounding syntax
is on the
Lexical structure
page.
Flags
| Flag | Property | Meaning | Since |
|---|---|---|---|
d | hasIndices | Adds start and end indices for each group to the match | ES2022 |
g | global | Find all matches; uses lastIndex | ES3 |
i | ignoreCase | Case-insensitive | ES3 |
m | multiline | ^ and $ match at line boundaries | ES3 |
s | dotAll | . also matches line terminators | ES2018 |
u | unicode | Unicode mode: code points, \u{…}, \p{…} and stricter syntax | ES2015 |
v | unicodeSets | Unicode mode with set notation and properties of strings | ES2024 |
y | sticky | Match only at lastIndex | ES2015 |
u and v may not be used together. Each flag may appear only
once, in any order. re.flags returns them sorted, so the order is
dgimsuvy.
Pattern grammar
Pattern:: Disjunction
Disjunction:: Alternative
Alternative | Disjunction
Alternative:: [empty]
Alternative Term
Term:: Assertion
Atom
Atom Quantifier
Assertion:: ^ $ \b \B
(?= Disjunction )
(?! Disjunction )
(?<= Disjunction )
(?<! Disjunction )
Quantifier:: QuantifierPrefix
QuantifierPrefix ?
QuantifierPrefix:: * + ? { DecimalDigits } { DecimalDigits ,} { DecimalDigits , DecimalDigits }
Atom:: PatternCharacter
.
\ AtomEscape
CharacterClass
( [GroupSpecifier] Disjunction )
(? RegularExpressionModifiers : Disjunction )
(? RegularExpressionModifiers - RegularExpressionModifiers : Disjunction )
SyntaxCharacter:: one of ^ $ \ . * + ? ( ) [ ] { } |
Atoms
| Syntax | Matches | Since |
|---|---|---|
| a literal character | Itself, if not a syntax character | ES3 |
. | Any character except line terminators, or any at all with s | ES3 |
(…) | A capturing group | ES3 |
(?:…) | A non-capturing group | ES3 |
(?<name>…) | A named group | ES2018 |
(?ims-ims:…) | A modifier group | ES2025 |
[…], [^…] | A character class | ES3 |
\1 to \9… | A backreference to that group | ES3 |
\k<name> | A backreference by name | ES2018 |
In Unicode mode (u or v), a character is a code point; without
it, a code unit. A backreference to a group that has not participated matches
the empty string, and forward references behave the same way.
Escapes
| Escape | Matches |
|---|---|
\d \D | ASCII digit [0-9], or not |
\w \W | Word character [A-Za-z0-9_], or not |
\s \S | White space and line terminators, or not |
\t \n \r \v \f | Tab, line feed, carriage return, vertical tab, form feed |
\0 | NUL, when no digit follows |
\cX | A control character, from an ASCII letter |
\xHH | A code unit |
\uHHHH | A code unit, or in Unicode mode a code point |
\u{H…} | A code point, in Unicode mode |
\p{…} \P{…} | A Unicode property, or its opposite |
\ + a syntax character | That character |
In Unicode mode, any other identity escape is a syntax error, which catches typing
mistakes. \d and \w stay ASCII-only, even with
u. Inside a class, \b means backspace.
Assertions
| Syntax | Tests | Since |
|---|---|---|
^ | Start of input, or of a line with m | ES3 |
$ | End of input, or of a line with m | ES3 |
\b, \B | A word boundary, or not | ES3 |
(?=…), (?!…) | Positive and negative lookahead | ES3 |
(?<=…), (?<!…) | Positive and negative lookbehind | ES2018 |
Lookaround consumes nothing. Lookbehind matches backwards, and captures taken in it reflect that.
Quantifiers
| Syntax | Repeats |
|---|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 |
{n} | Exactly n |
{n,} | n or more |
{n,m} | n to m |
Quantifiers are greedy by default and lazy with a trailing ?:
a+? prefers the fewest. There are no possessive quantifiers or atomic
groups. Matching uses backtracking, so a poorly written pattern can take exponential
time.
Character classes
/[aeiou]/ // one vowel
/[^0-9]/ // any non-digit
/[a-zA-Z_]/ // ranges
/[\s,;]/ // escapes work inside
- A
-at the start or end of a class is literal. - Without
uorv, a class works on code units, so it may split a surrogate pair. - A range’s start may not be above its end.
Set notation (v flag)
The v flag adds operations on classes:
| Syntax | Meaning |
|---|---|
[[a-z][0-9]] | Nested classes (union) |
[A--B] | Subtraction |
[A&&B] | Intersection |
[\q{abc|d|ef}] | Multi-character strings, tried longest first |
In a v class, these characters must be escaped:
( ) [ ] { } / - \ |. Doubled punctuators such as &&
!! ## $$ %% **
++ ,, .. :: ;;
<< == >> ??
@@ ^^ `` ~~ are reserved, and
may not appear unescaped.
/[\p{Script=Greek}--[α-ω]]/v // Greek letters except lower-case α to ω
/[\p{ASCII}&&\p{L}]/v // ASCII letters
/[\p{RGI_Emoji}]/v // emoji sequences
Unicode property escapes
\p{…} needs u or v (ES2018).
| Form | Example |
|---|---|
| General category | \p{L}, \p{Lu}, \p{General_Category=Letter} |
| Script | \p{Script=Latin}, \p{sc=Greek} |
| Script extensions | \p{Script_Extensions=Hira} |
| Binary property | \p{ASCII}, \p{Emoji}, \p{Alphabetic} |
Property of strings, v only | \p{RGI_Emoji}, \p{Basic_Emoji} |
Named groups
const m = /(?<year>\d{4})-(?<month>\d{2})/.exec('2026-11');
m.groups.year; // "2026"
'2026-11'.replace(/(?<y>\d+)-(?<m>\d+)/, '$<m>/$<y>'); // "11/2026"
Names follow identifier rules. A name may be used twice only in different
alternatives (ES2025), so only one can participate: (?<v>a)|(?<v>b).
groups is a null-prototype object.
Modifiers
(?flags:…) and (?flags-flags:…) turn i,
m or s on or off for part of a pattern (ES2025). Flags may
not repeat, and it is an error if both sides are empty.
/(?i:hello) world/.test('HELLO world'); // true, only "hello" ignores case
Results and methods
| Member | Result |
|---|---|
re.exec(s) | A match array or null |
re.test(s) | A boolean |
re.source, re.flags | The pattern and flags |
re.lastIndex | Where the next search starts, for g and y. Writable |
s.match(re) | With g, all matches as strings; without, the same as exec |
s.matchAll(re) | An iterator of match arrays. Needs g (ES2020) |
s.replace(re, x), s.replaceAll(re, x) | Substitutes. replaceAll needs g (ES2021) |
s.search(re) | Index of the first match, or −1 |
s.split(re) | Splits, including any captures |
RegExp.escape(s) | Escapes text so it can be used in a pattern (ES2025) |
A match array holds the whole match and each group, with these extra properties:
index, input, groups, and with the
d flag, indices holding [start, end] pairs.
Substitution patterns
| In the replacement | Means |
|---|---|
$$ | A literal $ |
$& | The whole match |
$`, $' | The text before and after the match |
$1 to $99 | A captured group |
$<name> | A named group |
A replacement function receives the match, the groups, the index, the input, and
the groups object if the pattern has named groups.
Annex B legacy syntax
Without u or v, Annex B permits older forms, such as a
bare { or ], octal escapes such as \012,
and a quantifier after a lookahead. RegExp.prototype.compile is also
in Annex B. The grammar above applies in Unicode mode.