JavaScript (ES2026) reference · String Methods
JavaScript String matchAll() Method
Return an iterator of every regular-expression match, including capture groups. The regex must have the g flag.
JavaScript (ES2026) Since ES2020
Syntax
string.matchAll(regexp)
Parameters
| Parameter | Description |
|---|---|
regexp |
A regular expression with the g flag. |
Return value
An iterator of match arrays.
Examples
const log = "user=ada id=7; user=linus id=9";
for (const m of log.matchAll(/user=(\w+) id=(\d+)/g)) {
console.log(m[1], Number(m[2]), "at", m.index);
}
Output
ada 7 at 0 linus 9 at 15
const tags = [..."#js #css #html".matchAll(/#(\w+)/g)].map((m) => m[1]);
console.log(tags);
Output
[ 'js', 'css', 'html' ]
Version history
| Version | Change |
|---|---|
| JavaScript ES2020 | Added |
Related
string.match()· Match the string against a regular expression and return the matches (all of them with the g flag) or null.string.replaceAll()· Replace every occurrence of a string (or regex with the g flag) with new text, without writing a regex.- JavaScript Strings · tutorial
- JavaScript String Methods · tutorial