String.prototype.matchAll
Returns an iterator of all results matching a string against a regular expression, including capturing groups
Syntax
string.matchAll(regexp)Parameters
| Parameter | Type | Description |
|---|---|---|
| regexp | RegExp | A regular expression with the global flag set |
Return Value
An iterable iterator of matches
Examples
const str = 'cat bat hat';
const matches = [...str.matchAll(/[cbh]at/g)];
matches.forEach(m => console.log(m[0]));
// cat, bat, hatconst text = 'key1=val1&key2=val2';
for (const match of text.matchAll(/(\w+)=(\w+)/g)) {
console.log(match[1], match[2]);
}
// key1 val1, key2 val2const html = '<div id="a"><span id="b">';
const ids = [...html.matchAll(/id="(\w+)"/g)].map(m => m[1]);
console.log(ids); // ['a', 'b']Understanding String.prototype.matchAll
The String.prototype.matchAll method in JavaScript returns an iterator of all results matching a string against a regular expression, including capturing groups. It belongs to the String object and is one of the most widely used methods for working with string values in modern JavaScript and TypeScript applications.
The method signature is string.matchAll(regexp). It accepts 1 parameter: regexp. When called, it returns an iterable iterator of matches. Understanding when and how to use matchAll() helps you write more expressive, readable code.
Common use cases for String.prototype.matchAll include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like string-match, string-replace, string-search, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for String.prototype.matchAll is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
String.prototype.matchRetrieves the result of matching a string against a regular expression
String.prototype.replaceReturns a new string with one or all matches of a pattern replaced by a replacement
String.prototype.searchExecutes a search for a match between a regular expression and this string, returning the index of the first match
RegExp.prototype[Symbol.matchAll]Returns an iterator of all results matching a string against a regular expression, including capturing groups
More String Methods
Other methods in the String object
Related Tools
More String Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.