Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

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 15

Group 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.2026

Notes

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

In the tutorial

  • Regular expressions — Regular expressions in JavaScript for beginners: patterns, flags, character classes, testing, finding and replacing text.

Thanks for reading!

Donate to the site — we work hard for you.