`match()` runs a regular expression against a string and returns matching text, rather than just a yes-or-no result. The shape of its result depends on whether the expression is global. section 12.3 section 12.4.2
Choose what to do with the result based on the expression:
- Without the `g` flag, `match()` returns an array containing the full match and any captured groups. For example, matching `/\d+/` against `"It's over 9000!"` puts `"9000"` at index 0. section 12.4.2 section 12.3
- With the `g` flag, `match()` returns an array of full matches; captured groups are not included. Use `matchAll()` when you need captures from each global match, or when you want to process results as an iterator. section 12.4.2
Extracting a number
Run `"It's over 9000!".match(/\d+/)` and check `found?.[0] === "9000"` to confirm the matched text. section 12.3
Listing 12.6 Finding multiple matches with a global regex
const ninjaRoster =
"Hattori of Iga, Yoshi of Koga, Fuma of Hojo";
const titleRegex = /([^\s,]+) of ([^\s,]+)/g; #A
for (const found of ninjaRoster.matchAll( #B
titleRegex, #B
)) { #B
const [, name, clan] = found; #C
console.log(
`${name} hails from the ${clan} clan`,
);
}
See a global-regex example that uses `matchAll()` to extract names and clans from each match. section 12.4.2


