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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

string.matchAll(regexp)

Parameters

ParameterDescription
regexp A regular expression with the g flag.

Return value

An iterator of match arrays.

Examples

Every match with groupsTry it yourself
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
Collect groups into an arrayTry it yourself
const tags = [..."#js #css #html".matchAll(/#(\w+)/g)].map((m) => m[1]);
console.log(tags);

Output

[ 'js', 'css', 'html' ]

Version history

VersionChange
JavaScript ES2020Added

Was this page helpful?