Named capture groups
Named groups — parts of a match with readable names.
Available since: ES2018
Syntax
(?<name>pattern)Examples
Parsing a date with named groups:
const match = '2026-03-15'.match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/);
console.log(match.groups.year, match.groups.month, match.groups.day);Output:
2026 03 15Group names in a replacement string:
const date = '2026-03-15';
console.log(date.replace(/(?<y>\d+)-(?<m>\d+)-(?<d>\d+)/, '$<d>.$<m>.$<y>'));Output:
15.03.2026Notes
The groups object is present on the results of match(), exec() and matchAll(). If a group did not take part in the match, its value is undefined.
See also
- RegExp.prototype.exec — Finds a match and returns the details: groups and position.
- String.prototype.match — Finds matches of a regular expression.
- String.prototype.replace — Replaces the first match (or all of them with a regex g flag).
In the tutorial
- Regular expressions — Regular expressions in JavaScript for beginners: patterns, flags, character classes, testing, finding and replacing text.