Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 38 ยท Groups, Backreferences and Lookaround

Named and Non-Capturing Groups

Make regexes readable with named capture groups and avoid unneeded captures with non-capturing groups. Covers groups objects, destructuring, duplicate names, replacement references and modifiers.

Numbered groups work, but m[3] tells you nothing, and adding one group at the start renumbers everything after it. Two features fix that: named groups give captures readable names, and non-capturing groups let you group without capturing at all.

Named groups

Write (?<name>...). The captured text is available as match.groups.name (and still at its number).

Node.js Named captures

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.

Names must be valid identifiers (letters, digits, _, $, not starting with a digit) and, in most engines, unique within a pattern. groups is an object with a null prototype, so it has no toString and no inherited properties.

Node.js Named groups with matchAll

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 a named group does not take part in the match, its property exists but is undefined, so you can always read groups.name safely.

Using names in the replacement

In replace, use $<name> in the replacement string, or read the last argument (the groups object) in a callback.

Node.js Named references in 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.

Non-capturing groups

(?:...) groups tokens without capturing. Use it whenever you need grouping only for a quantifier or an alternation.

Node.js Capturing versus non-capturing

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.

Non-capturing groups:

  • keep the group numbers of the groups you really care about small and stable,
  • are slightly faster and use less memory,
  • tell the reader: "this is grouping only".

A good rule: if you do not use the text of a group, make it (?:...). If you do use it, give it a name when the pattern has more than two groups.

Duplicate named groups

A named group can usually appear only once. Modern JavaScript engines (ES2025) allow the same name in different alternatives, so only one of them can participate.

Node.js The same name in two branches

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 your target environment is older, use different names and combine them in code: groups.d1 ?? groups.d2.

Modifiers: scoped flags

Recent engines let you turn flags on or off for part of a pattern: (?i:...) makes only that group case-insensitive, and (?-i:...) turns it off inside an i regex. This works in Node.js 24, so we can try it here.

Node.js Case-insensitive only for part

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.

Support for modifiers arrived recently, so check your target browsers before relying on them in shared code.

Choosing between the three

flowchart TD
  A["Need to group tokens?"] -->|"yes"| B{"Do you need the matched text?"}
  B -->|"no"| C["(?:...) non-capturing"]
  B -->|"yes, a few groups"| D["(...) numbered"]
  B -->|"yes, several or it must be readable"| E["(?<name>...) named"]

Capture only what you use, and name it when there is more than one.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you read a named group called year?

    Named captures live in the groups object of the match.
  2. 2. What does (?:ab)+ do compared with (ab)+?

    Non-capturing groups match exactly like capturing ones but do not store the text or take a number.
  3. 3. How do you reference a named group in a replacement string?

    In replace, $<name> inserts the named capture. \k<name> is for backreferences inside the pattern.
  4. 4. Why prefer non-capturing groups for pure grouping?

    Unneeded captures clutter results and shift the numbering when the pattern changes.

Summary

  • (?<name>...) captures into match.groups.name; use $<name> in replacements.
  • (?:...) groups without capturing; use it for quantifiers and alternations when you do not need the text.
  • Duplicate names are allowed only in different alternatives in modern engines.
  • (?i:...) scopes a flag to part of a pattern in recent engines.

Next: Backreferences