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
Example
#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)
}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
#string.matchAll(regexp)Parameters
#regexp
RegExp | string
A regular expression with thegflag. A plain string gets converted to an expression with thegflag.
More examples
#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}`))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]))match() with the g flag only gives back the whole matches. Only matchAll() pulls out the task numbers from the (\d+) group.
try {
'a1b2'.matchAll(/\d/)
} catch (error) {
console.log(error.name)
}
console.log([...'a1b2'.matchAll(/\d/g)].length)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 likematch[1]andmatch[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
#- String.prototype.match()Matches a string against a regular expression. Returns an array with the match or all matches, or null.
- RegExpA regular expression: a pattern for searching, validating, and replacing pieces of text.
- String.prototype.replaceAll()Returns a new string in which every occurrence of a pattern has been replaced with the given text or the result of a function.
- for...ofWalks the successive values of an iterable object: an array, a string, a Map, a Set, and others.
- Iterators (Symbol.iterator)The iteration protocol: an object with a [Symbol.iterator]() method works with for...of, spread, and destructuring.
Spotted a mistake or missing an example? Write to us.