String.prototype.matchAll()
Returns an iterator of all matches of a regular expression, including capture groups.
Syntax
str.matchAll(regexp) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
regexp | RegExp | Yes | A regular expression that must have the global (g) flag. |
Returns
Iterator — An iterator of match arrays, each with index, input, and named groups.
Throws
TypeError— the regexp does not have the global (g) flag.
Examples
const matches = [...'a1b2'.matchAll(/([a-z])(\d)/g)];
console.log(matches[0][1], matches[0][2]);
Output
a 1
for (const m of 'x=1;y=2'.matchAll(/(\w)=(\d)/g)) console.log(m[1], m[2]);
Output
x 1
y 2
Notes
The regex MUST have the g flag or a TypeError is thrown. Unlike `match` with g,
each result includes capture groups. Spread or iterate to collect results.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 73 |
| firefox | 67 |
| safari | 13 |
| edge | 79 |
| node | 12 |