Sub topic 11
Strings and Regular Expressions: match, matchAll, search
This chapter assumes you can read basic regular expression syntax, such as character classes, quantifiers, capture groups, and the g flag. You should also know indexOf from the searching chapter and replace from the previous chapter. Three string methods take a regular expression and report what it found:
matchreturns matched text.matchAllreturns full details for every match.searchreturns only a position.
match: one result or all the text
match behaves differently depending on whether the regex has the g flag. This difference causes most of the bugs with this method.
const log = "id=1 id=22 id=333";
const first = log.match(/id=(\d+)/);
console.log(first[0], first[1], first.index); // id=1 1 0
console.log(log.match(/\d+/g)); // ["1", "22", "333"]
console.log(log.match(/id=(\d+)/g)); // ["id=1", "id=22", "id=333"]
console.log("abc".match(/\d+/g)); // null
console.log("abc".match(/\d+/g) ?? []); // []When the regex has no g flag, match returns the first match as a detailed array. Element [0] holds the full match, the next elements hold the capture groups, and the index property holds the position. When the regex has the g flag, match returns a plain array of every full match and drops the capture groups. In both cases it returns null when nothing matches, not an empty array.
Named groups appear on the groups property of the non-global result:
const d = "2026-11-25".match(/(?<year>\d{4})-(?<month>\d{2})/);
console.log(d.groups.year, d.groups.month); // 2026 11matchAll: every match with its groups
When you need the capture groups for every match, use matchAll. It returns an iterator, and each item has the same shape as a non-global match result.
const log = "id=1 id=22 id=333";
for (const m of log.matchAll(/id=(\d+)/g)) {
console.log(m[1], m.index); // 1 0, then 22 5, then 333 11
}
const ids = [...log.matchAll(/id=(\d+)/g)].map((m) => Number(m[1]));
console.log(ids); // [1, 22, 333]matchAll requires the g flag. Passing a regex without it throws a TypeError. It never returns null. When nothing matches, the iterator is empty. An iterator can be consumed only once, so spread it into an array if you need to read the results twice.
search: just the position
search works like indexOf but takes a pattern. It returns the index of the first match, or -1 if there is none. It ignores the g flag and always starts from the beginning of the string.
console.log("id=1 id=22".search(/=/)); // 2
console.log("id=1 id=22".search(/\d{2}/)); // 8
console.log("hello".search(/\d/)); // -1Choosing a method
| Method | `g` flag | Returns | No match |
|---|---|---|---|
| `match` | absent | detailed array for the first match | `null` |
| `match` | present | array of matched strings | `null` |
| `matchAll` | required | iterator of detailed arrays | empty iterator |
| `search` | ignored | index number | `-1` |
For a plain yes/no check, /pattern/.test(str) is clearer than comparing search to -1.
Exercise
Given const tags = "#js #web #regex", write one expression that returns ["js", "web", "regex"], and explain why log.match(/#(\w+)/g) does not give that result.
Show answer
[...tags.matchAll(/#(\w+)/g)].map((m) => m[1]). With the g flag, match returns only the full matches ["#js", "#web", "#regex"] and drops the capture groups, so the "#" stays in each result.
Exercise
What do "a1b2".match(/\d/), "a1b2".search(/\d/g), and "ab".matchAll(/\d/) produce?
Show answer
The first returns a detailed array ["1"] with index 1. The second returns 1, because search ignores the g flag. The third throws a TypeError, because matchAll requires the g flag.