JavaScript matchAll()
Anonymous contributor
Published Jun 22, 2025
Contribute to Docs
The matchAll() method is a built-in JavaScript function that returns an iterator of all matches of a regular expression within a string. It provides detailed match results, including capturing groups and match indices.
Syntax
str.matchAll(regexp)
Parameters:
regexp: A regular expression object with the global (g) flag. If thegflag is not present, aTypeErroris thrown.
Return value:
An iterator of match result objects, each containing:
- the full matched substring
- any capturing groups
- the index of the match
- the input string
Example
In this example, matchAll() retrieves all regex matches from the string 'test1 test2', providing access to full matches and captured groups as part of a match array:
const text = 'test1 test2';const regex = /t(e)(st(\d?))/g;const matches = Array.from(text.matchAll(regex));console.log(matches[0]);console.log(matches[1][0]);
The output of this code will be:
['test1','e','st1','1',index: 0,input: 'test1 test2',groups: undefined]test2
Codebyte Example
In this example, matchAll() is used in a loop to find all words ending in 'at' and log their values along with their starting indices:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.