Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 38 ยท Syntax and Matching

Match Results, Indices and Iteration

Read JavaScript regex match objects, loop through all matches with exec and matchAll, control lastIndex with the g and y flags, and get exact match positions with the d flag.

So far you used test and match to see whether something matched. Real programs usually need more: where the match is, what the groups captured, and how to walk through all matches in a text. This lesson explains the match object and the different ways to iterate.

The match array

exec (and match without g) returns a special array with extra properties:

Node.js Anatomy of a match

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

When the pattern did not match at all, exec returns null, so always check before indexing: m?.[1] or if (m) { ... }.

Looping with exec and the g flag

A regex with the g flag keeps a lastIndex and continues from it on each exec. A while loop walks every match:

Node.js exec in a loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

An empty match (a pattern like /x*/g that can match zero characters) does not advance lastIndex, so a naive while loop runs forever. matchAll and replace handle this for you; with exec you must bump lastIndex manually when m[0] === ''.

matchAll: the clean way

matchAll returns an iterator of match objects, copies the regex internally (so your regex keeps its own lastIndex) and handles empty matches. It requires the g flag.

Node.js matchAll to array and for...of

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Sticky matching: the y flag

The y (sticky) flag makes the regex match only at lastIndex, with no searching ahead. It is the building block of tokenizers and parsers: each step must match exactly where the previous one ended.

Node.js A tiny tokenizer with y

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

If the text at lastIndex does not match, exec returns null and resets lastIndex to 0, so an unexpected character stops the loop instead of being skipped silently. The g flag would have skipped ahead and hidden the error.

Match positions: the d flag

The d flag (hasIndices) adds an indices array to each match with the start and end of the whole match and of every group. This is what editors need to highlight capture groups.

Node.js Group positions with d

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Surveying results: quick patterns

Node.js Counting, first, last, unique

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

flowchart TD
  A["Need all matches?"] -->|"no"| B["exec / match without g"]
  A -->|"yes, strings only"| C["match with g"]
  A -->|"yes, with groups and index"| D["matchAll"]
  D --> E["Need exact group positions?"]
  E -->|"yes"| F["add the d flag"]
  A -->|"tokenizing step by step"| G["exec with y flag"]

Choose the iteration tool by what you need from each match.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does m.index hold for /b/.exec('abc')?

    index is the zero-based position where the match starts; b is at position 1.
  2. 2. Why is matchAll usually better than an exec while-loop?

    matchAll is a safer iterator over all matches and avoids the shared lastIndex state.
  3. 3. What does the y flag do?

    Sticky matching starts exactly at lastIndex, which is how tokenizers read text step by step.
  4. 4. What extra data does the d flag add?

    With d, each match object has indices, and indices.groups for named groups.

Summary

  • A match is an array with the matched text, group captures and index, input, groups.
  • Use matchAll for iteration; with exec beware of lastIndex and empty matches.
  • The y flag anchors each attempt to lastIndex, perfect for tokenizers.
  • The d flag provides exact start and end offsets for the match and every group.

Next: Character Classes