JavaScript String.prototype.matchAll() Method
The matchAll() method returns an iterator of every regular expression match in a string, including capture groups for each one.
Description
The matchAll() method returns an iterator of all matches, where each match is an array including its capture groups, like a non-global match() result. The regular expression must have the g flag, or a TypeError is thrown.
Each match in the iterator has the same shape as a non-global match() result: the full match, the capture groups, index and groups for named groups. Because it's an iterator, you can loop over it with for...of, spread it into an array with [...str.matchAll(re)], or use Array.from() to map the matches in one step.
Unlike looping with exec(), matchAll() works on a copy of the regular expression, so it doesn't change the original's lastIndex, and there's no risk of an infinite loop from a pattern that matches an empty string.
Syntax
string.matchAll(regexp)Parameters
- regexpA regular expression with the
gflag.
Return Value
An iterator of match arrays, each with capture groups, index and groups.
Exceptions
- TypeErrorThrown if the regular expression doesn't have the
gflag.
Example
Browser Support
The following information will show you the current browser support for the String.prototype.matchAll() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





