Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 38 ยท Groups, Backreferences and Lookaround

Capturing Groups

Use parentheses in regular expressions to group tokens and capture text. Learn group numbering, nested groups, quantified groups, optional groups and how to read captures in JavaScript.

Parentheses do two jobs in a regex. They group several tokens so a quantifier or an alternation applies to all of them, and they capture the text that the group matched so you can read it afterwards. This lesson focuses on capturing: how the groups are numbered, what they hold, and the cases that surprise people.

Grouping and capturing

Node.js Capture the pieces of a date

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.

Groups are numbered by the position of their opening parenthesis, counting from left to right, starting at 1. Group 0 is always the whole match.

Nested groups

With nesting, count opening parentheses in the order they appear in the pattern.

Node.js Nested numbering

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
  G0["Group 0: whole match"] --> G1["Group 1: ((\\d{3})-(\\d{4}))"]
  G1 --> G2["Group 2: (\\d{3})"]
  G1 --> G3["Group 3: (\\d{4})"]

Numbers follow the opening parentheses from left to right; inner groups are numbered after the outer one that contains them.

Quantifiers on groups

A quantifier after a group repeats the whole group. But the group holds only the text of its last repetition.

Node.js Only the last repetition is kept

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.

This is a frequent mistake: (\d+,)+ does not give you a list of the numbers, it gives you just the last one. To get all of them, match the whole run with one group and split it later, or loop with matchAll.

Optional groups

If a group does not take part in the match, its slot is undefined.

Node.js Groups that did not participate

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.

Always handle undefined for optional groups (m[2] ?? 'default'). An empty string and undefined mean different things: the group matched nothing versus the group was skipped.

Reading captures with matchAll and replace

You rarely call exec by hand. matchAll gives you captures for every match, and replace with a function receives them as arguments.

Node.js Captures in matchAll and replace

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.

Captures with split

String.split with a regex that contains a capturing group keeps the separators in the result array. That can be handy, or a surprise.

Node.js split keeps captured separators

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.

Group count and performance

Every capturing group costs a little memory and time, and shifts the numbers of later groups when you add or remove one. The next lesson shows non-capturing groups (?:...) and named groups (?<name>...) that fix both problems. Use plain ( ) only for pieces you will actually read.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In /((a)(b))/ what does group 2 hold for 'ab'?

    Groups are numbered by their opening parenthesis: 1 is the outer, 2 is (a), 3 is (b).
  2. 2. What is m[1] for /(\w)+/.exec('abc')?

    A repeated group keeps only the text of its last repetition.
  3. 3. What is the value of a group that did not take part in the match?

    Non-participating groups are undefined, which differs from a group that matched the empty string.
  4. 4. What does 'a1b'.split(/(\d)/) return?

    Capturing groups in a split pattern add the captured separators to the result.

Summary

  • ( ) groups tokens and captures their text; groups are numbered by opening parenthesis, group 0 is the whole match.
  • A quantified group keeps only its last repetition.
  • Groups that did not participate are undefined.
  • matchAll, replace callbacks and split all expose captures.

Next: Named and Non-Capturing Groups