String.prototype.matchAll()

str.matchAll(regexp)
Returns: Iterator · Added in vES2020 · Updated March 13, 2026 · String Methods
string regex match matchall

The matchAll() method returns an iterator of all matches of a string against a regular expression, making it easier to work with capture groups.

Syntax

str.matchAll(regexp)

Parameters

ParameterTypeDescription
regexpRegExpA regular expression object. Must include the /g flag.

Examples

Basic usage

const text = "The quick brown fox jumps over the lazy dog";

const matches = [...text.matchAll(/o/g)];

console.log(matches[0]);
// ['o', index: 12, input: 'The quick brown fox...', groups: undefined]

console.log(matches.length);
// 4

Working with capture groups

const dates = "2026-03-09, 2025-12-25, 2024-01-01";

// Extract year, month, day using capture groups
const regex = /(\d{4})-(\d{2})-(\d{2})/g;
const matches = [...dates.matchAll(regex)];

console.log(matches[0]);
// ['2026-03-09', '2026', '03', '09', index: 0, input: '2026-03-09...']

// Iterate over all matches
for (const match of dates.matchAll(regex)) {
  console.log(`Year: ${match[1]}, Month: ${match[2]}, Day: ${match[3]}`);
}
// Year: 2026, Month: 03, Day: 09
// Year: 2025, Month: 12, Day: 25
// Year: 2024, Month: 01, Day: 01

Named capture groups

const log = "2026-03-09 14:30:45 [INFO] Server started";

// Named capture groups (ES2018+)
const pattern = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/g;

for (const match of log.matchAll(pattern)) {
  console.log(match.groups);
// { year: '2026', month: '03', day: '09' }
}

Common Patterns

Using matchAll with destructuring

const data = "id:1,name:Alice,id:2,name:Bob";
const pattern = /id:(?<id>\d+),name:(?<name>\w+)/g;

const users = [...data.matchAll(pattern)].map(m => m.groups);
console.log(users);
// [{ id: '1', name: 'Alice' }, { id: '2', name: 'Bob' }]

Efficient processing

const largeText = "..."; // large string

// matchAll returns an iterator - memory efficient
const regex = /pattern/g;
const matchIterator = largeText.matchAll(regex);

// Process one at a time
let match;
while (!(match = regex.exec(largeText)).done) {
  // process match
}

match() vs matchAll()

Featurematch()matchAll()
ReturnsArray or nullIterator
Capture groupsAccessibleAccessible with indices
/g flag requiredNoYes (always)
ReusableNo (creates new array)Yes (iterator)

See Also