JavaScriptStrings

String.prototype.matchAll()

Returns an iterator over all the matches of a regular expression, each one together with its groups and position in the text.

Returns
An iterator of arrays with the matches. When nothing matches, the iterator is empty.
Changes the original
No
On this page

Example

#
JavaScript
const report = 'HTML: 120 pts, CSS: 95 pts, JS: 140 pts'

for (const match of report.matchAll(/(\w+): (\d+) pts/g)) {
  console.log(match[1], Number(match[2]), match.index)
}
Console
HTML 120 0
CSS 95 15
JS 140 28

Definition and usage

#

The matchAll() method finds every match of a regular expression in a string and returns them as an iterator. Each element is an array just like the one from match() without the g flag: index 0 holds the whole matched text, the following indexes hold the capture groups, and the index and groups properties hold the position and named groups.

It solves a problem match() cannot handle. With the g flag you get all the matches, but without groups, and without the flag you get the groups of only the first match. matchAll() gives you both at once.

The expression must have the g flag, or the method throws a TypeError. The result is an iterator, not an array: you can loop over it with for...of, and turn it into an array with [...text.matchAll(re)] or Array.from(). An iterator can only be walked through once.

Syntax

#
Syntax
string.matchAll(regexp)

Parameters

#
  • regexp

    RegExp | string

    A regular expression with the g flag. A plain string gets converted to an expression with the g flag.

More examples

#
Named groups and an array of results
JavaScript
const text = 'Dates: 2026-10-05, 2026-11-20'
const dates = [...text.matchAll(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/g)]

console.log(dates.length)
console.log(dates.map((match) => `${match.groups.day}.${match.groups.month}`))
Console
2
['05.10', '20.11']
Comparison with match()
JavaScript
const text = 'Task 3 for 15 pts, task 7 for 40 pts'
const re = /task (\d+)/gi

console.log(text.match(re))
console.log([...text.matchAll(re)].map((match) => match[1]))
Console
['Task 3', 'task 7']
['3', '7']

match() with the g flag only gives back the whole matches. Only matchAll() pulls out the task numbers from the (\d+) group.

Trap: missing the g flag
JavaScript
try {
  'a1b2'.matchAll(/\d/)
} catch (error) {
  console.log(error.name)
}

console.log([...'a1b2'.matchAll(/\d/g)].length)
Console
TypeError
2

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • An iterator from matchAll() can only be walked through once. If you need the results more than once, convert it to an array right away: [...text.matchAll(re)].
  • Named groups (?<name>...) are easier to read than references like match[1] and match[2], especially when there are several groups.
  • If you only need the matched pieces themselves, without groups or positions, the simpler text.match(/.../g) is enough.

Related entries

#

Spotted a mistake or missing an example? Write to us.