HomepageExploring JavaScript (ES2024 Edition)
You can support this book: buy it or donate
(Ad, please don’t block.)

45 Regular expressions (RegExp)

Icon “reading”Availability of features

Unless stated otherwise, each regular expression feature has been available since ES3.

45.1 Creating regular expressions

45.1.1 Literal vs. constructor

The two main ways of creating regular expressions are:

Both regular expressions have the same two parts:

45.1.2 Cloning and non-destructively modifying regular expressions

There are two variants of the constructor RegExp():

The second variant is useful for cloning regular expressions, optionally while modifying them. Flags are immutable and this is the only way of changing them – for example:

function copyAndAddFlags(regExp, flagsToAdd='') {
  // The constructor doesn’t allow duplicate flags;
  // make sure there aren’t any:
  const newFlags = Array.from(
    new Set(regExp.flags + flagsToAdd)
  ).join('');
  return new RegExp(regExp, newFlags);
}
assert.equal(/abc/i.flags, 'i');
assert.equal(copyAndAddFlags(/abc/i, 'g').flags, 'gi');

45.2 Syntax characters and escaping

45.2.1 Syntax characters

At the top level of a regular expression, the following syntax characters are special. They are escaped by prefixing a backslash (\).

\ ^ $ . * + ? ( ) [ ] { } |

In regular expression literals, we must escape slashes:

> /\//.test('/')
true

In the argument of new RegExp(), we don’t have to escape slashes:

> new RegExp('/').test('/')
true

45.2.2 Illegal top-level escaping

Without flag /u and /v, an escaped non-syntax character at the top level matches itself:

> /^\a$/.test('a')
true

With flag /u or /v, escaping a non-syntax character at the top level is a syntax error:

assert.throws(
  () => eval(String.raw`/\a/v`),
  {
    name: 'SyntaxError',
    message: 'Invalid regular expression: /\\a/v: Invalid escape',
  }
);
assert.throws(
  () => eval(String.raw`/\-/v`),
  {
    name: 'SyntaxError',
    message: 'Invalid regular expression: /\\-/v: Invalid escape',
  }
);

45.2.3 Escaping inside character classes ([···])

Rules for escaping inside character classes without flag /v:

Rules with flag /v:

45.3 Syntax: atoms of regular expressions

Atoms are the basic building blocks of regular expressions.

Without /u and /v, a character is a UTF-16 code unit. With those flags, a character is a code point.

45.4 Syntax: character class escapes

45.4.1 Basic character class escapes (sets of code units): \d \D \s \S \w \W

The following character class escapes and their complements are always supported:

EscapeEquivalentComplement
Digits\d[0-9]\D
“Word” characters\w[a-zA-Z0-9_]\W
Whitespace\s\S

Note:

Examples:

> 'a7x4'.match(/\d/g)
[ '7', '4' ]
> 'a7x4'.match(/\D/g)
[ 'a', 'x' ]
> 'high - low'.match(/\w+/g)
[ 'high', 'low' ]
> 'hello\t\n everyone'.replaceAll(/\s/g, '-')
'hello---everyone'

45.4.2 Unicode character property escapes [ES2018]

With flag /u and flag /v, we can use \p{} and \P{} to specify sets of code points via Unicode character properties (we’ll learn more about those in the next subsection). That looks like this:

  1. \p{prop=value}: matches all characters whose Unicode character property prop has the value value.
  2. \P{prop=value}: matches all characters that do not have a Unicode character property prop whose value is value.
  3. \p{bin_prop}: matches all characters whose binary Unicode character property bin_prop is True.
  4. \P{bin_prop}: matches all characters whose binary Unicode character property bin_prop is False.

Comments:

Examples:

45.4.2.1 Unicode character properties

In the Unicode standard, each character has properties – metadata describing it. Properties play an important role in defining the nature of a character. Quoting the Unicode Standard, Sect. 3.3, D3:

The semantics of a character are determined by its identity, normative properties, and behavior.

These are a few examples of properties:

Further reading:

45.4.3 Unicode string property escapes [ES2024]

With /u, we can use Unicode property escapes (\p{} and \P{}) to specify sets of code points via Unicode character properties.

With /v, we can additionally use \p{} to specify sets of code point sequences via Unicode string properties (negation via \P{} is not supported):

> /^\p{RGI_Emoji}$/v.test('⛔') // 1 code point (1 code unit)
true
> /^\p{RGI_Emoji}$/v.test('🙂') // 1 code point (2 code units)
true
> /^\p{RGI_Emoji}$/v.test('😵‍💫') // 3 code points
true

Let’s see how the character property Emoji would do with these inputs:

> /^\p{Emoji}$/u.test('⛔') // 1 code point (1 code unit)
true
> /^\p{Emoji}$/u.test('🙂') // 1 code point (2 code units)
true
> /^\p{Emoji}$/u.test('😵‍💫') // 3 code points
false
45.4.3.1 Unicode string properties

For now, the following Unicode properties of strings are supported by JavaScript:

Further reading:

45.5 Syntax: character classes

A character class wraps class ranges in square brackets. The class ranges specify a set of characters:

Rules for class ranges:

45.5.1 String literals in character classes [ES2024]

Flag /v enables a new feature inside character classes – we can use \q{} to add code points sequences to their character sets:

> /^[\q{😵‍💫}]$/v.test('😵‍💫')
true

Without \q{}, grapheme clusters are still treated as several units:

> /^[😵‍💫]$/v.test('😵‍💫')
false
> /^[\u{1F635}\u{200D}\u{1F4AB}]$/v.test('😵‍💫') // equivalent
false
> /^[😵‍💫]$/v.test('\u{1F635}')
true

We can use a single \q{} to add multiple code point sequences – if we separate them with pipes:

> /^[\q{abc|def}]$/v.test('abc')
true
> /^[\q{abc|def}]$/v.test('def')
true

45.5.2 Set operations for character classes [ES2024]

Flag /v enables set operations for character classes.

45.5.2.1 Nesting character classes

To enable set operations for character classes, we must be able to nest them. Character class escapes already provides some kind of nesting:

> /^[\d\w]$/v.test('7')
true
> /^[\d\w]$/v.test('H')
true
> /^[\d\w]$/v.test('?')
false

With flag /v, we can additionally nest character classes (the regular expression below is equivalent to the regular expression in the previous example):

> /^[[0-9][A-Za-z0-9_]]$/v.test('7')
true
> /^[[0-9][A-Za-z0-9_]]$/v.test('H')
true
> /^[[0-9][A-Za-z0-9_]]$/v.test('?')
false
45.5.2.2 Subtraction of character sets via --

We can use the -- operator to set-theoretically subtract the character sets defined by character classes or character class escapes:

> /^[\w--[a-g]]$/v.test('a')
false
> /^[\w--[a-g]]$/v.test('h')
true

> /^[\p{Number}--[0-9]]$/v.test('٣')
true
> /^[\p{Number}--[0-9]]$/v.test('3')
false

> /^[\p{RGI_Emoji}--\q{😵‍💫}]$/v.test('😵‍💫') // emoji has 3 code points
false
> /^[\p{RGI_Emoji}--\q{😵‍💫}]$/v.test('🙂')
true

Single code points can also be used on either side of the -- operator:

> /^[\w--a]$/v.test('a')
false
> /^[\w--a]$/v.test('b')
true
45.5.2.3 Intersection of character sets via &&

We can use the && operator to set-theoretically intersect the character sets defined by character classes or character class escapes:

> /[\p{ASCII}&&\p{Letter}]/v.test('D')
true
> /[\p{ASCII}&&\p{Letter}]/v.test('Δ')
false

> /^[\p{Script=Arabic}&&\p{Number}]$/v.test('٣')
true
> /^[\p{Script=Arabic}&&\p{Number}]$/v.test('ج')
false
45.5.2.4 Union of characters sets

Two compute the set-theoretical union of character sets, we only need to write their definining constructs next to each other inside a character class:

> /^[\p{Emoji_Keycap_Sequence}[a-z]]+$/v.test('a2️⃣c')
true

45.6 Syntax: capture groups

45.7 Syntax: quantifiers

By default, all of the following quantifiers are greedy (they match as many characters as possible):

To make them reluctant (so that they match as few characters as possible), put question marks (?) after them:

> /".*"/.exec('"abc"def"')[0]  // greedy
'"abc"def"'
> /".*?"/.exec('"abc"def"')[0] // reluctant
'"abc"'

45.8 Syntax: assertions

Overview of available lookaround assertions:

PatternName
(?=«pattern»)Positive lookaheadES3
(?!«pattern»)Negative lookaheadES3
(?<=«pattern»)Positive lookbehindES2018
(?<!«pattern»)Negative lookbehindES2018

45.8.1 Lookahead assertions

Positive lookahead: (?=«pattern») matches if pattern matches what comes next.

Example: sequences of lowercase letters that are followed by an X.

> 'abcX def'.match(/[a-z]+(?=X)/g)
[ 'abc' ]

Note that the X itself is not part of the matched substring.

Negative lookahead: (?!«pattern») matches if pattern does not match what comes next.

Example: sequences of lowercase letters that are not followed by an X.

> 'abcX def'.match(/[a-z]+(?!X)/g)
[ 'ab', 'def' ]

45.8.2 Lookbehind assertions [ES2018]

Positive lookbehind: (?<=«pattern») matches if pattern matches what came before.

Example: sequences of lowercase letters that are preceded by an X.

> 'Xabc def'.match(/(?<=X)[a-z]+/g)
[ 'abc' ]

Negative lookbehind: (?<!«pattern») matches if pattern does not match what came before.

Example: sequences of lowercase letters that are not preceded by an X.

> 'Xabc def'.match(/(?<!X)[a-z]+/g)
[ 'bc', 'def' ]

Example: replace “.js” with “.html”, but not in “Node.js”.

> 'Node.js: index.js and main.js'.replace(/(?<!Node)\.js/g, '.html')
'Node.js: index.html and main.html'

45.9 Syntax: disjunction (|)

Caveat: this operator has low precedence. Use groups if necessary:

45.10 Regular expression flags

Literal flagProperty nameESDescription
dhasIndicesES2022Switch on match indices
gglobalES3Match multiple times
iignoreCaseES3Match case-insensitively
mmultilineES3^ and $ match per line
sdotAllES2018Dot matches line terminators
uunicodeES6Unicode mode
vunicodeSetsES2024Unicode sets mode (recommended)
ystickyES6No characters between matches

Table 45.1: These are the regular expression flags supported by JavaScript.

The following regular expression flags are available in JavaScript (table 45.1 provides a compact overview):

45.10.1 How to order regular expression flags?

Consider the following regular expression: /“([^”]+)”/udg

In which order should we list its flags? Two options are:

  1. Alphabetical order: /dgu
  2. In order of importance (arguably, /u is most fundamental etc.): /ugd

Given that (2) is not obvious, (1) is the better choice. JavaScript also uses it for the RegExp property .flags :

> /-/gymdivs.flags
'dgimsvy'

45.10.2 Without /u and /v: matching UTF-16 code units

Without the flags /u and /v, most constructs work with single UTF-16 code units – which is a problem whenever there is a code point with two code units – such as 🙂:

> '🙂'.length
2

We can use code unit escapes – \u followed by four hexadecimal digits:

> /^\uD83D\uDE42$/.test('🙂')
true

The dot operator (.) matches code units:

> '🙂'.match(/./g)
[ '\uD83D', '\uDE42' ]

Quantifiers apply to code units:

> /^🙂{2}$/.test('\uD83D\uDE42\uDE42')
true
> /^\uD83D\uDE42{2}$/.test('\uD83D\uDE42\uDE42') // equivalent
true

Character class escapes define sets of code units:

> '🙂'.match(/\D/g)
[ '\uD83D', '\uDE42' ]

Character classes define sets of code units:

> /^[🙂]$/.test('🙂')
false
> /^[\uD83D\uDE42]$/.test('\uD83D\uDE42') // equivalent
false
> /^[🙂]$/.test('\uD83D')
true

45.10.3 Flag /u: matching code points [ES6]

In the previous subsection, we encountered problems when we wanted to match a code point with more than one UTF-16 code unit – such as 🙂. Flag /u enables support for this kind of code point and fixes those problems.

We can use code point escapes – \u{} with one to six hexadecimal digits:

> /^\u{1F642}$/u.test('🙂')
true

The dot operator (.) matches code points:

> '🙂'.match(/./gu)
[ '🙂' ]

Quantifiers apply to code points:

> /^🙂{2}$/u.test('🙂🙂')
true

Character class escapes define sets of code points:

> '🙂'.match(/\D/gu)
[ '🙂' ]

A new kind of character class escapes is supported – Unicode character property escapes:

> /^\p{Emoji}$/u.test('⛔') // 1 code point (1 code unit)
true
> /^\p{Emoji}$/u.test('🙂') // 1 code point (2 code units)
true

Character classes define sets of code points:

> /^[🙂]$/u.test('🙂')
true
> /^[🙂]$/u.test('\uD83D')
false

45.10.4 Flag /v: limited support for multi-code-point grapheme clusters [ES2024]

Icon “tip”Use flag /v whenever you can

This flag improves many aspects of JavaScript’s regular expressions and should be used by default. If you can’t use it yet because it’s still too new, you can use /u, instead.

45.10.4.1 Limitation of flag /u: handling grapheme clusters with more than one code point

Some font glyphs are represented by grapheme clusters (code point sequences) with more than one code point – e.g. 😵‍💫:

> Array.from('😵‍💫').length // count code points
3

Flag /u does not help us with those kinds of grapheme clusters:

// Grapheme cluster is not matched by single dot
assert.equal(
  '😵‍💫'.match(/./gu).length, 3
);

// Quantifiers only repeat last code point of grapheme cluster
assert.equal(
  /^😵‍💫{2}$/u.test('😵‍💫😵‍💫'), false
);

// Character class escapes only match single code points
assert.equal(
  /^\p{Emoji}$/u.test('😵‍💫'), false
);

// Character classes only match single code points
assert.equal(
  /^[😵‍💫]$/u.test('😵‍💫'), false
);
45.10.4.2 Flag /v: Unicode string property escapes and character class string literals

Flag /v works like flag /u but provides better support for multi-code-point grapheme clusters. It doesn’t switch from code points to grapheme clusters everywhere, but it does fix the last two issues we encountered in the previous subsection – by adding support for multi-code-point grapheme clusters to:

45.10.4.3 Flag /v: character class set operations

Character classes can be nested and combined via the set operations subtraction and intersection – see “Set operations for character classes [ES2024]” (§45.5.2).

45.10.4.4 Flag /v: improved case-insensitive matching

Flag /u has a quirk when it comes to case-insensitive matching: Using \P{···} produces different results than [^\p{···}]:

> /^\P{Lowercase_Letter}$/iu.test('A')
true
> /^\P{Lowercase_Letter}$/iu.test('a')
true

> /^[^\p{Lowercase_Letter}]$/iu.test('A')
false
> /^[^\p{Lowercase_Letter}]$/iu.test('a')
false

Observations:

Flag /v fixes that quirk:

> /^\P{Lowercase_Letter}$/iv.test('A')
false
> /^\P{Lowercase_Letter}$/iv.test('a')
false

> /^[^\p{Lowercase_Letter}]$/iv.test('A')
false
> /^[^\p{Lowercase_Letter}]$/iv.test('a')
false

Further reading:

45.11 Properties of regular expression objects

Noteworthy:

45.11.1 Flags as properties

Each regular expression flag exists as a property with a longer, more descriptive name:

> /a/i.ignoreCase
true
> /a/.ignoreCase
false

This is the complete list of flag properties:

45.11.2 Other properties

Each regular expression also has the following properties:

45.12 Match objects

Several regular expression-related methods return so-called match objects to provide detailed information for the locations where a regular expression matches an input string. These methods are:

This is an example:

assert.deepEqual(
  /(a+)b/d.exec('ab aaab'),
  {
    0: 'ab',
    1: 'a',
    index: 0,
    input: 'ab aaab',
    groups: undefined,
    indices: {
      0: [0, 2],
      1: [0, 1],
      groups: undefined
    },
  }
);

The result of .exec() is a match object for the first match with the following properties:

45.12.1 Match indices in match objects [ES2022]

Match indices are a feature of match objects: If we turn it on via the regular expression flag /d (property .hasIndices), they record the start and end indices of where groups were captured.

45.12.1.1 Match indices for numbered groups

This is how we access the captures of numbered groups:

const matchObj = /(a+)(b+)/d.exec('aaaabb');
assert.equal(
  matchObj[1], 'aaaa'
);
assert.equal(
  matchObj[2], 'bb'
);

Due to the regular expression flag /d, matchObj also has a property .indices that records for each numbered group where it was captured in the input string:

assert.deepEqual(
  matchObj.indices[1], [0, 4]
);
assert.deepEqual(
  matchObj.indices[2], [4, 6]
);
45.12.1.2 Match indices for named groups

The captures of named groups are accessed likes this:

const matchObj = /(?<as>a+)(?<bs>b+)/d.exec('aaaabb');
assert.equal(
  matchObj.groups.as, 'aaaa');
assert.equal(
  matchObj.groups.bs, 'bb');

Their indices are stored in matchObj.indices.groups:

assert.deepEqual(
  matchObj.indices.groups.as, [0, 4]);
assert.deepEqual(
  matchObj.indices.groups.bs, [4, 6]);
45.12.1.3 A more realistic example

One important use case for match indices are parsers that point to where exactly a syntactic error is located. The following code solves a related problem: It points to where quoted content starts and where it ends (see demonstration at the end).

const reQuoted = /“([^”]+)”/dgu;
function pointToQuotedText(str) {
  const startIndices = new Set();
  const endIndices = new Set();
  for (const match of str.matchAll(reQuoted)) {
    const [start, end] = match.indices[1];
    startIndices.add(start);
    endIndices.add(end);
  }
  let result = '';
  for (let index=0; index < str.length; index++) {
    if (startIndices.has(index)) {
      result += '[';
    } else if (endIndices.has(index+1)) {
      result += ']';
    } else {
      result += ' ';
    }
  }
  return result;
}

assert.equal(
  pointToQuotedText(
    'They said “hello” and “goodbye”.'),
    '           [   ]       [     ]  '
);

45.13 Methods for working with regular expressions

45.13.1 By default, regular expressions match anywhere in a string

By default, regular expressions match anywhere in a string:

> /a/.test('__a__')
true

We can change that by using assertions such as ^ or by using the flag /y:

> /^a/.test('__a__')
false
> /^a/.test('a__')
true

45.13.2 regExp.test(str): is there a match? [ES3]

The regular expression method .test() returns true if regExp matches str:

> /bc/.test('ABCD')
false
> /bc/i.test('ABCD')
true
> /\.mjs$/.test('main.mjs')
true

With .test() we should normally avoid the /g flag. If we use it, we generally don’t get the same result every time we call the method:

> const r = /a/g;
> r.test('aab')
true
> r.test('aab')
true
> r.test('aab')
false

The results are due to /a/ having two matches in the string. After all of those were found, .test() returns false.

45.13.3 str.search(regExp): at what index is the match? [ES3]

The string method .search() returns the first index of str at which there is a match for regExp:

> '_abc_'.search(/abc/)
1
> 'main.mjs'.search(/\.mjs$/)
4

45.13.4 regExp.exec(str): capturing groups [ES3]

45.13.4.1 Getting a match object for the first match

Without the flag /g, .exec() returns a match object for the first match of regExp in str:

assert.deepEqual(
  /(a+)b/.exec('ab aab'),
  {
    0: 'ab',
    1: 'a',
    index: 0,
    input: 'ab aab',
    groups: undefined,
  }
);
45.13.4.2 Named capture groups [ES2018]

The previous example contained a single numbered group. The following example demonstrates named groups:

assert.deepEqual(
  /(?<as>a+)b/.exec('ab aab'),
  {
    0: 'ab',
    1: 'a',
    index: 0,
    input: 'ab aab',
    groups: { as: 'a' },
  }
);

In the result of .exec(), we can see that a named group is also a numbered group – its capture exists twice:

45.13.4.3 Looping over all matches

Icon “tip”Better alternative for retrieving all matches: str.matchAll(regExp) [ES2020]

Since ECMAScript 2020, JavaScript has another method for retrieving all matches: str.matchAll(regExp). That method is easier to use and has fewer caveats.

If we want to retrieve all matches of a regular expression (not just the first one), we need to switch on the flag /g. Then we can call .exec() multiple times and get one match each time. After the last match, .exec() returns null.

> const regExp = /(a+)b/g;
> regExp.exec('ab aab')
{ 0: 'ab', 1: 'a', index: 0, input: 'ab aab', groups: undefined }
> regExp.exec('ab aab')
{ 0: 'aab', 1: 'aa', index: 3, input: 'ab aab', groups: undefined }
> regExp.exec('ab aab')
null

Therefore, we can loop over all matches as follows:

const regExp = /(a+)b/g;
const str = 'ab aab';

let match;
// Check for null via truthiness
// Alternative: while ((match = regExp.exec(str)) !== null)
while (match = regExp.exec(str)) {
  console.log(match[1]);
}

Output:

a
aa

Icon “warning”Be careful when sharing regular expressions with /g!

Sharing regular expressions with /g has a few pitfalls, which are explained later.

Icon “exercise”Exercise: Extracting quoted text via .exec()

exercises/regexps/extract_quoted_test.mjs

45.13.5 str.match(regExp): getting all group 0 captures [ES3]

Without /g, .match() works like .exec() – it returns a single match object.

With /g, .match() returns all substrings of str that match regExp:

> 'ab aab'.match(/(a+)b/g)
[ 'ab', 'aab' ]

If there is no match, .match() returns null:

> 'xyz'.match(/(a+)b/g)
null

We can use the nullish coalescing operator (??) to protect ourselves against null:

const numberOfMatches = (str.match(regExp) ?? []).length;

45.13.6 str.matchAll(regExp): getting an iterable over all match objects [ES2020]

This is how .matchAll() is invoked:

const matchIterable = str.matchAll(regExp);

Given a string and a regular expression, .matchAll() returns an iterable over the match objects of all matches.

In the following example, we use Array.from() to convert iterables to Arrays so that we can compare them better.

> Array.from('-a-a-a'.matchAll(/-(a)/ug))
[
  { 0:'-a', 1:'a', index: 0, input: '-a-a-a', groups: undefined },
  { 0:'-a', 1:'a', index: 2, input: '-a-a-a', groups: undefined },
  { 0:'-a', 1:'a', index: 4, input: '-a-a-a', groups: undefined },
]

Flag /g must be set:

> Array.from('-a-a-a'.matchAll(/-(a)/u))
TypeError: String.prototype.matchAll called with a non-global
RegExp argument

.matchAll() isn’t affected by regExp.lastIndex and doesn’t change it.

45.13.6.1 Implementing .matchAll()

.matchAll() could be implemented via .exec() as follows:

function* matchAll(str, regExp) {
  if (!regExp.global) {
    throw new TypeError('Flag /g must be set!');
  }
  const localCopy = new RegExp(regExp, regExp.flags);
  let match;
  while (match = localCopy.exec(str)) {
    yield match;
  }
}

Making a local copy ensures two things:

Using matchAll():

const str = '"fee" "fi" "fo" "fum"';
const regex = /"([^"]*)"/g;

for (const match of matchAll(str, regex)) {
  console.log(match[1]);
}

Output:

fee
fi
fo
fum

45.13.7 regExp.exec() vs. str.match() vs. str.matchAll()

The following table summarizes the differences between three methods:

Without /gWith /g
regExp.exec(str)First match objectNext match object or null
str.match(regExp)First match objectArray of group 0 captures
str.matchAll(regExp)TypeErrorIterable over match objects

45.13.8 Replacing with str.replace() and str.replaceAll()

Both replacing methods have two parameters:

searchValue can be:

replacementValue can be:

The two methods differ as follows:

This table summarizes how that works:

Search for: →stringRegExp w/o /gRegExp with /g
.replaceFirst occurrenceFirst occurrence(All occurrences)
.replaceAllAll occurrencesTypeErrorAll occurrences

The last column of .replace() is in parentheses because this method existed long before .replaceAll() and therefore supports functionality that should now be handled via the latter method. If we could change that, .replace() would throw a TypeError here.

We first explore how .replace() and .replaceAll() work individually when replacementValue is a simple string (without the character $). Then we examine how both are affected by more complicated replacement values.

45.13.8.1 str.replace(searchValue, replacementValue) [ES3]

How .replace() operates is influenced by its first parameter searchValue:

If we want to replace every occurrence of a string, we have two options:

45.13.8.2 str.replaceAll(searchValue, replacementValue) [ES2021]

How .replaceAll() operates is influenced by its first parameter searchValue:

45.13.8.3 The parameter replacementValue of .replace() and .replaceAll()

So far, we have only used the parameter replacementValue with simple strings, but it can do more. If its value is:

45.13.8.4 replacementValue is a string

If the replacement value is a string, the dollar sign has special meaning – it inserts text matched by the regular expression:

TextResult
$$single $
$&complete match
$`text before match
$'text after match
$ncapture of numbered group n (n > 0)
$<name>capture of named group name [ES2018]

Example: Inserting the text before, inside, and after the matched substring.

> 'a1 a2'.replaceAll(/a/g, "($`|$&|$')")
'(|a|1 a2)1 (a1 |a|2)2'

Example: Inserting the captures of numbered groups.

> const regExp = /^([A-Za-z]+): (.*)$/ug;
> 'first: Jane'.replaceAll(regExp, 'KEY: $1, VALUE: $2')
'KEY: first, VALUE: Jane'

Example: Inserting the captures of named groups.

> const regExp = /^(?<key>[A-Za-z]+): (?<value>.*)$/ug;
> 'first: Jane'.replaceAll(regExp, 'KEY: $<key>, VALUE: $<value>')
'KEY: first, VALUE: Jane'

Icon “exercise”Exercise: Change quotes via .replace() and a named group

exercises/regexps/change_quotes_test.mjs

45.13.8.5 replacementValue is a function

If the replacement value is a function, we can compute each replacement. In the following example, we multiply each non-negative integer that we find by two.

assert.equal(
  '3 cats and 4 dogs'.replaceAll(/[0-9]+/g, (all) => 2 * Number(all)),
  '6 cats and 8 dogs'
);

The replacement function gets the following parameters. Note how similar they are to match objects. These parameters are all positional, but I’ve included how one might name them:

If we are only interested in groups, we can use the following technique:

const result = 'first=jane, last=doe'.replace(
  /(?<key>[a-z]+)=(?<value>[a-z]+)/g,
  (...args) => { // (A)
    const groups = args.at(-1); // (B)
    const {key, value} = groups;
    return key.toUpperCase() + '=' + value.toUpperCase();
  });
assert.equal(result, 'FIRST=JANE, LAST=DOE');

Due to the rest parameter in line A, args contains an Array with all parameters. We access the last parameter via the Array method .at() in line B.

45.13.9 Other methods for working with regular expressions

String.prototype.split() is described in the chapter on strings. Its first parameter of String.prototype.split() is either a string or a regular expression. If it is the latter, then captures of groups appear in the result:

> 'a:b : c'.split(':')
[ 'a', 'b ', ' c' ]
> 'a:b : c'.split(/ *: */)
[ 'a', 'b', 'c' ]
> 'a:b : c'.split(/( *):( *)/)
[ 'a', '', '', 'b', ' ', ' ', 'c' ]

45.14 The flags /g and /y, and the property .lastIndex (advanced)

In this section, we examine how the RegExp flags /g and /y work and how they depend on the RegExp property .lastIndex. We’ll also discover an interesting use case for .lastIndex that you may find surprising.

45.14.1 The flags /g and /y

Every method reacts differently to /g and /y; this gives us a rough general idea:

If a regular expression has neither the flag /g nor the flag /y, matching happens once and starts at the beginning.

With either /g or /y, matching is performed relative to a “current position” inside the input string. That position is stored in the regular expression property .lastIndex.

There are three groups of regular-expression-related methods:

  1. The string methods .search(regExp) and .split(regExp) completely ignore /g and /y (and therefore also .lastIndex).

  2. The RegExp methods .exec(str) and .test(str) change in two ways if either /g or /y is set.

    First, we get multiple matches, by calling one method repeatedly. Each time, it returns either another result (a match object or true) or an “end of results” value (null or false).

    Second, the regular expression property .lastIndex is used to step through the input string. On one hand, .lastIndex determines where matching starts:

    • /g means that a match must begin at .lastIndex or later.

    • /y means that a match must begin at .lastIndex. That is, the beginning of the regular expression is anchored to .lastIndex.

      Note that ^ and $ continue to work as usually: They anchor matches to the beginning or end of the input string, unless .multiline is set. Then they anchor to the beginnings or ends of lines.

    On the other hand, .lastIndex is set to one plus the last index of the previous match.

  3. All other methods are affected as follows:

    • /g leads to multiple matches.
    • /y leads to a single match that must start at .lastIndex.
    • /yg leads to multiple matches without gaps.

This was a first overview. The next sections get into more details.

45.14.2 How exactly are methods affected by /g and /y?

45.14.2.1 regExp.exec(str) [ES3]

Without /g and /y, .exec() ignores .lastIndex and always returns a match object for the first match:

> const re = /#/; re.lastIndex = 1;
> [re.exec('##-#'), re.lastIndex]
[{ 0: '#', index: 0, input: '##-#' }, 1]
> [re.exec('##-#'), re.lastIndex]
[{ 0: '#', index: 0, input: '##-#' }, 1]

With /g, the match must start at .lastIndex or later. .lastIndex is updated. If there is no match, null is returned.

> const re = /#/g; re.lastIndex = 1;
> [re.exec('##-#'), re.lastIndex]
[{ 0: '#', index: 1, input: '##-#' }, 2]
> [re.exec('##-#'), re.lastIndex]
[{ 0: '#', index: 3, input: '##-#' }, 4]
> [re.exec('##-#'), re.lastIndex]
[null, 0]

With /y, the match must start at exactly .lastIndex. .lastIndex is updated. If there is no match, null is returned.

> const re = /#/y; re.lastIndex = 1;
> [re.exec('##-#'), re.lastIndex]
[{ 0: '#', index: 1, input: '##-#' }, 2]
> [re.exec('##-#'), re.lastIndex]
[null, 0]

With /yg, .exec() behaves the same as with /y.

45.14.2.2 regExp.test(str) [ES3]

This method behaves the same same as .exec(), but instead of returning a match object, it returns true, and instead of returning null, it returns false.

For example, without either /g or /y, the result is always true:

> const re = /#/; re.lastIndex = 1;
> [re.test('##-#'), re.lastIndex]
[true, 1]
> [re.test('##-#'), re.lastIndex]
[true, 1]

With /g, there are two matches:

> const re = /#/g; re.lastIndex = 1;
> [re.test('##-#'), re.lastIndex]
[true, 2]
> [re.test('##-#'), re.lastIndex]
[true, 4]
> [re.test('##-#'), re.lastIndex]
[false, 0]

With /y, there is only one match:

> const re = /#/y; re.lastIndex = 1;
> [re.test('##-#'), re.lastIndex]
[true, 2]
> [re.test('##-#'), re.lastIndex]
[false, 0]

With /yg, .test() behaves the same as with /y.

45.14.2.3 str.match(regExp) [ES3]

Without /g, .match() works like .exec(). Either without /y:

> const re = /#/; re.lastIndex = 1;
> ['##-#'.match(re), re.lastIndex]
[{ 0: '#', index: 0, input: '##-#' }, 1]
> ['##-#'.match(re), re.lastIndex]
[{ 0: '#', index: 0, input: '##-#' }, 1]

Or with /y:

> const re = /#/y; re.lastIndex = 1;
> ['##-#'.match(re), re.lastIndex]
[{ 0: '#', index: 1, input: '##-#' }, 2]
> ['##-#'.match(re), re.lastIndex]
[null, 0]

With /g, we get all matches (group 0) in an Array. .lastIndex is ignored and reset to zero.

> const re = /#/g; re.lastIndex = 1;
> '##-#'.match(re)
['#', '#', '#']
> re.lastIndex
0

/yg works similarly to /g, but no gaps between matches are allowed:

> const re = /#/yg; re.lastIndex = 1;
> '##-#'.match(re)
['#', '#']
> re.lastIndex
0
45.14.2.4 str.matchAll(regExp) [ES2020]

If /g is not set, .matchAll() throws an exception:

> const re = /#/y; re.lastIndex = 1;
> '##-#'.matchAll(re)
TypeError: String.prototype.matchAll called with
a non-global RegExp argument

If /g is set, matching starts at .lastIndex and that property isn’t changed:

> const re = /#/g; re.lastIndex = 1;
> Array.from('##-#'.matchAll(re))
[
  { 0: '#', index: 1, input: '##-#' },
  { 0: '#', index: 3, input: '##-#' },
]
> re.lastIndex
1

/yg works similarly to /g, but no gaps between matches are allowed:

> const re = /#/yg; re.lastIndex = 1;
> Array.from('##-#'.matchAll(re))
[
  { 0: '#', index: 1, input: '##-#' },
]
> re.lastIndex
1
45.14.2.5 str.replace(regExp, str) [ES3]

Without /g and /y, only the first occurrence is replaced:

> const re = /#/; re.lastIndex = 1;
> '##-#'.replace(re, 'x')
'x#-#'
> re.lastIndex
1

With /g, all occurrences are replaced. .lastIndex is ignored but reset to zero.

> const re = /#/g; re.lastIndex = 1;
> '##-#'.replace(re, 'x')
'xx-x'
> re.lastIndex
0

With /y, only the (first) occurrence at .lastIndex is replaced. .lastIndex is updated.

> const re = /#/y; re.lastIndex = 1;
> '##-#'.replace(re, 'x')
'#x-#'
> re.lastIndex
2

/yg works like /g, but gaps between matches are not allowed:

> const re = /#/yg; re.lastIndex = 1;
> '##-#'.replace(re, 'x')
'xx-#'
> re.lastIndex
0
45.14.2.6 str.replaceAll(regExp, str) [ES2021]

.replaceAll() works like .replace() but throws an exception if /g is not set:

> const re = /#/y; re.lastIndex = 1;
> '##-#'.replaceAll(re, 'x')
TypeError: String.prototype.replaceAll called
with a non-global RegExp argument

45.14.3 Four pitfalls of /g and /y and how to deal with them

We will first look at four pitfalls of /g and /y and then at ways of dealing with those pitfalls.

45.14.3.1 Pitfall 1: We can’t inline a regular expression with /g or /y

A regular expression with /g can’t be inlined. For example, in the following while loop, the regular expression is created fresh, every time the condition is checked. Therefore, its .lastIndex is always zero and the loop never terminates.

let matchObj;
// Infinite loop
while (matchObj = /a+/g.exec('bbbaabaaa')) {
  console.log(matchObj[0]);
}

With /y, the problem is the same.

45.14.3.2 Pitfall 2: Removing /g or /y can break code

If code expects a regular expression with /g and has a loop over the results of .exec() or .test(), then a regular expression without /g can cause an infinite loop:

function collectMatches(regExp, str) {
  const matches = [];
  let matchObj;
  // Infinite loop
  while (matchObj = regExp.exec(str)) {
    matches.push(matchObj[0]);
  }
  return matches;
}
collectMatches(/a+/, 'bbbaabaaa'); // Missing: flag /g

Why is there an infinite loop? Because .exec() always returns the first result, a match object, and never null.

With /y, the problem is the same.

45.14.3.3 Pitfall 3: Adding /g or /y can break code

With .test(), there is another caveat: It is affected by .lastIndex. Therefore, if we want to check exactly once if a regular expression matches a string, then the regular expression must not have /g. Otherwise, we generally get a different result every time we call .test():

> const regExp = /^X/g;
> [regExp.test('Xa'), regExp.lastIndex]
[ true, 1 ]
> [regExp.test('Xa'), regExp.lastIndex]
[ false, 0 ]
> [regExp.test('Xa'), regExp.lastIndex]
[ true, 1 ]

The first invocation produces a match and updates .lastIndex. The second invocation does not find a match and resets .lastIndex to zero.

If we create a regular expression specifically for .test(), then we probably won’t add /g. However, the likeliness of encountering /g increases if we use the same regular expression for replacing and for testing.

Once again, this problem also exists with /y:

> const regExp = /^X/y;
> regExp.test('Xa')
true
> regExp.test('Xa')
false
> regExp.test('Xa')
true
45.14.3.4 Pitfall 4: Code can produce unexpected results if .lastIndex isn’t zero

Given all the regular expression operations that are affected by .lastIndex, we must be careful with many algorithms that .lastIndex is zero at the beginning. Otherwise, we may get unexpected results:

function countMatches(regExp, str) {
  let count = 0;
  while (regExp.test(str)) {
    count++;
  }
  return count;
}

const myRegExp = /a/g;
myRegExp.lastIndex = 4;
assert.equal(
  countMatches(myRegExp, 'babaa'), 1); // should be 3

Normally, .lastIndex is zero in newly created regular expressions and we won’t change it explicitly like we did in the example. But .lastIndex can still end up not being zero if we use the regular expression multiple times.

45.14.3.5 How to avoid the pitfalls of /g and /y

As an example of dealing with /g and .lastIndex, we revisit countMatches() from the previous example. How do we prevent a wrong regular expression from breaking our code? Let’s look at three approaches.

45.14.3.5.1 Throwing exceptions

First, we can throw an exception if /g isn’t set or .lastIndex isn’t zero:

function countMatches(regExp, str) {
  if (!regExp.global) {
    throw new Error('Flag /g of regExp must be set');
  }
  if (regExp.lastIndex !== 0) {
    throw new Error('regExp.lastIndex must be zero');
  }
  
  let count = 0;
  while (regExp.test(str)) {
    count++;
  }
  return count;
}
45.14.3.5.2 Cloning regular expressions

Second, we can clone the parameter. That has the added benefit that regExp won’t be changed.

function countMatches(regExp, str) {
  const cloneFlags = regExp.flags + (regExp.global ? '' : 'g');
  const clone = new RegExp(regExp, cloneFlags);

  let count = 0;
  while (clone.test(str)) {
    count++;
  }
  return count;
}
45.14.3.5.3 Using an operation that isn’t affected by .lastIndex or flags

Several regular expression operations are not affected by .lastIndex or by flags. For example, .match() ignores .lastIndex if /g is present:

function countMatches(regExp, str) {
  if (!regExp.global) {
    throw new Error('Flag /g of regExp must be set');
  }
  return (str.match(regExp) ?? []).length;
}

const myRegExp = /a/g;
myRegExp.lastIndex = 4;
assert.equal(countMatches(myRegExp, 'babaa'), 3); // OK!

Here, countMatches() works even though we didn’t check or fix .lastIndex.

45.14.4 Use case for .lastIndex: starting matching at a given index

Apart from storing state, .lastIndex can also be used to start matching at a given index. This section describes how.

45.14.4.1 Example: Checking if a regular expression matches at a given index

Given that .test() is affected by /y and .lastIndex, we can use it to check if a regular expression regExp matches a string str at a given index:

function matchesStringAt(regExp, str, index) {
  if (!regExp.sticky) {
    throw new Error('Flag /y of regExp must be set');
  }
  regExp.lastIndex = index;
  return regExp.test(str);
}
assert.equal(
  matchesStringAt(/x+/y, 'aaxxx', 0), false);
assert.equal(
  matchesStringAt(/x+/y, 'aaxxx', 2), true);

regExp is anchored to .lastIndex due to /y.

Note that we must not use the assertion ^ which would anchor regExp to the beginning of the input string.

45.14.4.2 Example: Finding the location of a match, starting at a given index

.search() lets us find the location where a regular expression matches:

> '#--#'.search(/#/)
0

Alas, we can’t change where .search() starts looking for matches. As a workaround, we can use .exec() for searching:

function searchAt(regExp, str, index) {
  if (!regExp.global && !regExp.sticky) {
    throw new Error('Either flag /g or flag /y of regExp must be set');
  }
  regExp.lastIndex = index;
  const match = regExp.exec(str);
  if (match) {
    return match.index;
  } else {
    return -1;
  }
}

assert.equal(
  searchAt(/#/g, '#--#', 0), 0);
assert.equal(
  searchAt(/#/g, '#--#', 1), 3);
45.14.4.3 Example: Replacing an occurrence at a given index

When used without /g and with /y, .replace() makes one replacement – if there is a match at .lastIndex:

function replaceOnceAt(str, regExp, replacement, index) {
  if (!(regExp.sticky && !regExp.global)) {
    throw new Error('Flag /y must be set, flag /g must not be set');
  }
  regExp.lastIndex = index;
  return str.replace(regExp, replacement);
}
assert.equal(
  replaceOnceAt('aa aaaa a', /a+/y, 'X', 0), 'X aaaa a');
assert.equal(
  replaceOnceAt('aa aaaa a', /a+/y, 'X', 3), 'aa X a');
assert.equal(
  replaceOnceAt('aa aaaa a', /a+/y, 'X', 8), 'aa aaaa X');

45.14.5 The downsides of .lastIndex

The regular expression property .lastIndex has two significant downsides:

On the upside, .lastIndex also gives us additional useful functionality: We can dictate where matching should begin (for some operations).

45.14.6 Summary: .global (/g) and .sticky (/y)

The following two methods are completely unaffected by /g and /y:

This table explains how the remaining regular-expression-related methods are affected by these two flags:

r.lastIndex = 2Resultr.lastIndex
exec/#/.exec("##-#"){i:0}
/#/g.exec("##-#"){i:3}4
/#/y.exec("##-#")null0
test/#/.test("##-#")true
/#/g.test("##-#")true4
/#/y.test("##-#")false0
match"##-#".match(/#/){i:0}
"##-#".match(/#/g)["#","#","#"]0
"##-#".match(/#/y)null0
"##-#".match(/#/gy)["#","#"]0
matchAll"##-#".matchAll(/#/)TypeError
"##-#".matchAll(/#/g)<{i:3}>
"##-#".matchAll(/#/y)TypeError
"##-#".matchAll(/#/gy)<>
replace"##-#".replace(/#/, "x")"x#-#"
"##-#".replace(/#/g, "x")"xx-x"0
"##-#".replace(/#/y, "x")"##-#"0
"##-#".replace(/#/gy, "x")"xx-#"0
replaceAll"##-#".replaceAll(/#/, "x")TypeError
"##-#".replaceAll(/#/g, "x")"xx-x"0
"##-#".replaceAll(/#/y, "x")TypeError
"##-#".replaceAll(/#/gy, "x")"xx-#"0

Abbreviations:

Icon “external”The Node.js script that generated the previous table

The previous table was generated via a Node.js script.

45.15 Techniques for working with regular expressions

45.15.1 Escaping arbitrary text for regular expressions

The following function escapes an arbitrary text so that it is matched verbatim if we put it inside a regular expression (except inside character classes ([···])):

function escapeForRegExp(str) {
  return str.replace(/[\\^$.*+?\(\)\[\]\{\}\|]/gv, '\\$&'); // (A)
}
assert.equal(escapeForRegExp('[yes?]'), String.raw`\[yes\?\]`);
assert.equal(escapeForRegExp('_g_'), String.raw`_g_`);

In line A, we escape all syntax characters. We have to be selective because the regular expression flags /u and /v forbid many escapes – see “Syntax characters and escaping” (§45.2). Examples: \a \: \-

escapeForRegExp() has two use cases:

.replace() only lets us replace plain text once. With escapeForRegExp(), we can work around that limitation:

const plainText = ':-)';
const regExp = new RegExp(escapeForRegExp(plainText), 'ug');
assert.equal(
  ':-) :-) :-)'.replace(regExp, '🙂'), '🙂 🙂 🙂'
);

If you have more complicated requirements such as escaping plain text inside character classes, you can take a look at the polyfill for the ECMAScript proposal “RegExp.escape().

45.15.2 Matching everything or nothing

Sometimes, we may need a regular expression that matches everything or nothing – for example, as a default value.

Regular expression literals can’t be empty because // starts a single-line comment. Therefore, the first of the previous two regular expressions is used in this case:

> new RegExp('')
/(?:)/

45.15.3 Using a tagged template to write regular expressions that are easier to understand

For more information, see “Tag function library: regex” (§23.4.2).