Named Groups
Numbered groups work, but m[4] tells a reader nothing — and adding a group earlier in the pattern silently changes every number after it. Named groups fix both problems: write (?<name>...) and read the capture by name.
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/g
Start 2025-03-01, end 2025-06-302 matches
#1 "2025-03-01" at index 6
group year: "2025"
group month: "03"
group day: "01"
#2 "2025-06-30" at index 22
group year: "2025"
group month: "06"
group day: "30"
Named groups are still numbered too: year is also group 1, month group 2, and so on. You can mix named and unnamed groups; numbers are assigned to all capturing groups from left to right.
Naming rules
| Rule | Example |
|---|---|
| Names must be valid identifiers | (?<user_id>...) ok, (?<user-id>...) error |
| Names must be unique in JavaScript* | two (?<x>...) → SyntaxError |
| Case-sensitive | Year and year are different |
* Newer JavaScript engines (ES2025) allow the same name in different alternatives, e.g. (?<y>\d{4})-\d\d|\d\d\/(?<y>\d{4}). Older engines reject it.
Reading named groups in JavaScript
The match object has a groups property:
const re = /(?<user>[\w.]+)@(?<domain>[\w.-]+)/;
const m = "Reply to thu.le@uni.edu.vn today".match(re);
console.log(m.groups.user);
console.log(m.groups.domain);
const { user, domain } = m.groups;
console.log(`${user} at ${domain}`);thu.le
uni.edu.vn
thu.le at uni.edu.vn
With matchAll you get one match object per hit:
const log = "cpu=73 mem=41 disk=88";
for (const m of log.matchAll(/(?<key>\w+)=(?<value>\d+)/g)) {
console.log(m.groups.key.padEnd(5), Number(m.groups.value));
}cpu 73
mem 41
disk 88
Named groups in replacements
In a JavaScript replacement string, lt;name> inserts a named group:
const s = "2025-03-01";
console.log(s.replace(/(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})/, "lt;d>.lt;m>.lt;y>"));01.03.2025
Syntax differs between languages
Named groups exist almost everywhere, but the spelling varies:
| Language | Define | Use in pattern | Use in replacement | Read in code |
|---|---|---|---|---|
| JavaScript | (?<n>...) |
\k<n> |
lt;n> |
m.groups.n |
| Python | (?P<n>...) |
(?P=n) |
\g<n> |
m.group("n") / m["n"] |
| Java | (?<n>...) |
\k<n> |
${n} |
m.group("n") |
| .NET (C#) | (?<n>...) or (?'n'...) |
\k<n> |
${n} |
m.Groups["n"].Value |
| Go | (?P<n>...) or (?<n>...) |
not supported | ${n} |
re.SubexpIndex("n") |
| Rust | (?P<n>...) or (?<n>...) |
not supported | ${n} / $n |
caps["n"] / caps.name("n") |
Python's classic (?P<name>...) syntax works in every Python 3 version:
import re
m = re.search(r"(?P<user>[\w.]+)@(?P<domain>[\w.-]+)", "Reply to thu.le@uni.edu.vn")
print(m.group("user"), m["domain"])
print(m.groupdict())
print(re.sub(r"(?P<y>\d{4})-(?P<m>\d\d)", r"\g<m>/\g<y>", "2025-03"))thu.le uni.edu.vn
{'user': 'thu.le', 'domain': 'uni.edu.vn'}
03/2025
Tip: Name the groups you read in code and make every other group non-capturing. Your future self will thank you.
Test yourself
How do you read the year group from a JavaScript match m?
m.yearm.groups.yearm.group('year')m['$year']
In JavaScript, how do you insert a named group d in a replacement string?
\g<d>${d}lt;d>(?P=d)
Does a named group also get a number?
- No, never
- Yes, counted with all capturing groups left to right
- Only in Python
- Only if it is the first group