String.matchAll()
Iterates all regex matches (with groups) using a global regex.
for (const m of string.matchAll(/pattern/g)) { /* m[0], m[1]... */ }This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Iterates all regex matches (with groups) using a global regex.
The matchAll() method returns an iterator of all matches found in a string when compared against a regular expression, specifically including capturing groups for each match. Unlike the global match() method, which returns only the match strings and discards capturing groups, matchAll() provides the same detailed match object as RegExp.prototype.exec() for every match. This makes it significantly more powerful for parsing structured text without requiring a manual 'while' loop. Since the return value is a lazy RegExpStringIterator, it offers better performance and lower memory overhead when dealing with massive datasets, as it does not instantiate all results at once. A critical constraint is that the provided regular expression must include the global (/g) flag; omitting it will result in a TypeError. IE is not supported. It is commonly used with the spread operator or Array.from() to generate an array of match objects.
Quick reference
Syntax
for (const m of string.matchAll(/pattern/g)) { /* m[0], m[1]... */ }
Inputs
Parameters
See it in practice
Examples
Extracting Groups into an Array
const str = 'price: $10.50, price: $20.00';
const regex = /price: \$(\d+\.\d{2})/g;
const results = [...str.matchAll(regex)];
console.log(results[0][1]);
console.log(results[1][1]);"10.50" "20.00"
Using the spread operator to convert the iterator into an array of match objects, allowing access to capture groups (index 1).
Iterating with for...of
const text = 'test1, test2, test3';
const regex = /test(\d)/g;
for (const match of text.matchAll(regex)) {
console.log('Found ' + match[0] + ' at index ' + match.index);
}"Found test1 at index 0" "Found test2 at index 7" "Found test3 at index 14"
Directly iterating over the result of matchAll(). This is memory efficient because it does not create the full array of matches in memory at once.
Using Named Capturing Groups
const dateStr = '2023-10-01, 2024-05-15';
const dateRegex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/g;
const dates = Array.from(dateStr.matchAll(dateRegex));
console.log(dates[0].groups.year);
console.log(dates[1].groups.month);"2023" "05"
matchAll() is the standard way to retrieve all matches including named capturing groups in a single operation.
Debug faster
Common Errors
TypeError
Cause: Attempting to call matchAll() with a RegExp that does not have the global (/g) flag.
Fix: Always append the 'g' flag to the regular expression when using it with matchAll().
'hello'.matchAll(/l/); // Throws TypeErrorLogicError
Cause: Attempting to iterate over the returned iterator more than once.
Fix: Iterators are exhausted after one traversal. Convert the result to an array using Array.from() if multiple passes are required.
const matches = str.matchAll(/regex/g);
for (const m of matches) {} // First pass works
for (const m of matches) {} // Second pass is emptyRuntime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Iterates all regex matches (with groups) using a global regex.
regexp: A RegExp with the global flag (/g).
TypeError: Always append the 'g' flag to the regular expression when using it with matchAll(). LogicError: Iterators are exhausted after one traversal. Convert the result to an array using Array.from() if multiple passes are required.