Named capture groups

Captures substrings under a name for readable access via match.groups.

Since ES2018 Spec ↗

Syntax

/(?<name>pattern)/

Examples

const m = /(?<year>\d{4})-(?<month>\d{2})/.exec("2026-05");
console.log(m.groups.year, m.groups.month);
Output
2026 05
const s = "2026-05-15".replace(
  /(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})/,
  "$<d>/$<m>/$<y>"
);
console.log(s);
Output
15/05/2026
const { groups } = "key=val".match(/(?<k>\w+)=(?<v>\w+)/);
console.log(groups.k, groups.v);
Output
key val

Notes

- Named groups appear in the match result's `groups` object. - Reference them in `replace()` with `$<name>` and back-reference within the pattern with `\k<name>`.

Browser & runtime support

EnvironmentSince version
chrome 64
firefox 78
safari 11.1
edge 79
node 10.0

See also