JavaScript String matchAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.matchAll(regexp) returns an iterator of every regex match, including capturing groups. It needs the g flag (ES2020) and does not mutate the string.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call string.matchAll. Start with this line.

JavaScript
str.matchAll(regexp)
CallWhat it does
str.matchAll(regexp)Primary signature.
string.matchAll(...)Call with the arguments your case needs.

Built into JavaScript. Call matchAll(...) on a string (or String instance) — no import required.

Library declarations
JavaScript
str.matchAll(regexp)

Parameters

ParameterDescription
regexp with gRequired. Pattern; missing g throws
Consume iteratorRequired. for...of, spread, or Array.from

Return Value

Return ValueDescription
*Iterator of exec()-shaped arrays

JavaScript Examples

Example 1 — All Matches with Capturing Groups

Spread matchAll() to get every hit with its groups.

JavaScript
const regexp = /t(e)(st(\d?))/g;
const str = "test1test2";

const array = [...str.matchAll(regexp)];

console.log(array[0][0], array[0][1], array[0][3]); // test1 e 1
console.log(array[1][0], array[1][1], array[1][3]); // test2 e 2
Try It Yourself

How It Works

  1. Build a global regex. /t(e)(st(\d?))/g has three capturing groups and the required g flag.
  2. Scan the string. matchAll finds "test1" then "test2".
  3. Read each hit. Index 0 is the full match; later indexes are groups ("e", "st1", "1").
  4. Spread to an array. [...] collects both match arrays so you can index them.

Example 2 — Loop Matches with for...of

Walk every match without a while (exec) loop.

JavaScript
const regexp = /foo[a-z]*/g;
const str = "table football, foosball";

for (const match of str.matchAll(regexp)) {
  console.log(match[0], "at", match.index);
}
// football at 6
// foosball at 16
Try It Yourself

How It Works

  1. Use a global pattern. /foo[a-z]*/g matches words that start with foo.
  2. Iterate the iterator. for...of yields one match array at a time.
  3. Read position. match[0] is the full string; match.index is where it starts.
  4. Need a second pass? Call matchAll again — the first iterator is spent.

Example 3 — matchAll() vs match() with g

Global match() drops groups; matchAll() keeps them.

JavaScript
const regexp = /t(e)(st(\d?))/g;
const str = "test1test2";

console.log(str.match(regexp));
// ["test1", "test2"]  — groups missing

const array = [...str.matchAll(regexp)];
console.log(array[0][0], array[0][1], array[0][3]); // test1 e 1
console.log(array[1][0], array[1][1], array[1][3]); // test2 e 2
Try It Yourself

How It Works

  1. Same regex, two APIs. Both use g, but the return shapes differ.
  2. Global match is flat. You only get full match strings — no group columns.
  3. matchAll keeps structure. Each hit still has "e", "st1", "1", and so on.
  4. Rule of thumb. Need groups on every hit? Use matchAll. Full strings only? match + g is enough.

Browser & Runtime Support

String.prototype.matchAll() is part of ES2020 and is Baseline Widely available across modern browsers since early 2020.

✓ Baseline · ES2020

String.prototype.matchAll()

Supported in Chrome 73+, Firefox 67+, Safari 13+, Edge 79+, and Node.js 12+. Not available in Internet Explorer.

95% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
String.matchAll() Excellent

Bottom line: Safe for modern web apps and current Node LTS releases. Always include the g flag. For very old targets, feature-detect with if ("matchAll" in String.prototype) or fall back to an exec() loop.

Frequently Asked Questions

matchAll(regexp) returns an iterator of every match. Each match looks like RegExp.exec() — full match, capturing groups, index, and input.
If you pass a RegExp, it must include the global g flag. Without g, matchAll() throws TypeError. Plain strings are converted with new RegExp(value, "g").
With g, match() returns only full match strings and drops capturing groups. matchAll() keeps groups for every hit. Prefer matchAll when groups matter.
No. It returns an empty iterator. Spreading or Array.from gives []. That differs from match(), which returns null on a miss.
No. After for...of finishes, the iterator is spent. Call matchAll() again for a fresh scan.
No. It clones the regex, so your original regexp.lastIndex stays the same — unlike looping with exec() and g.

Next: normalize()

Learn how to normalize Unicode strings into a consistent form.

normalize() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful