Level 3: Advanced · Lesson 21 of 30
Named Groups
Named groups such as (?<year>\d{4}) label captures with meaningful names. Your code reads groups.year instead of guessing what group 3 was.
Numbered groups work, but they age badly. A pattern with five captures forces every reader to count parentheses to learn that $4 is the minute and $5 is the second. Add one more group near the start and every number shifts. Named groups solve this by giving each capture a label.
In JavaScript the syntax is (?<name>...). The pattern (?<year>\d{4})-(?<month>\d{2}) captures the year and month under those names. In code, the match object exposes them as match.groups.year and match.groups.month. Names make the pattern self-documenting and the calling code much easier to read.
Named groups are still numbered. In the example above, year is also group 1 and month is group 2, so $1 keeps working. In a JavaScript replacement you can also refer to them by name with $<year>, which survives later edits to the pattern far better than a number does.
A group name must be a valid identifier: start with a letter or underscore and continue with letters, digits or underscores. Each name may appear only once in a pattern in most engines. Choose short, descriptive names such as user, domain, major or level.
Syntax differs between languages. JavaScript, C#, Ruby, PHP and Java use (?<name>...), though Java allows only letters and digits in the name. Python's re module uses (?P<name>...) only. Go and Rust traditionally used (?P<name>...) and have added the (?<name>...) form in newer versions. RegexLab's converter rewrites the syntax for you when you switch the target language.
Named groups shine when you extract several fields from structured text: versions like v1.2.3, key and value pairs, log lines, or email parts. Instead of returning an array and remembering positions, your code gets an object with clear property names, which you can pass straight to the next step of your program.
Use names for any pattern with more than two captures, or any pattern that other people will maintain. The extra characters cost nothing at runtime and save real time during review.
Live tester
2 matches (JavaScript engine)
Reports for 2026-03 and 2025-12 are ready.
Examples
Numbers
/(?<h>\d{2}):(?<m>\d{2})/Meet at 09:30 or 14:05.
Matches: “09:30”, “14:05”
Hours and minutes are captured as `h` and `m`, so code can read them by name.
Text
/(?<first>[A-Z]\w+) (?<last>Test)/Ali Test and Sara Test joined.
Matches: “Ali Test”, “Sara Test”
Each person is split into `first` and `last` named captures.
Codes
/(?<prefix>[A-Z]{3})-(?<num>\d{4})/Tickets INV-0042 and ORD-1234 are open.
Matches: “INV-0042”, “ORD-1234”
The letter prefix and the number are captured under readable names.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match semantic versions like `v1.2.3`, capturing `major`, `minor` and `patch` by name.
Shipped v1.2.3 and v10.0.1, not version 4
Task 2: Match `key=value` pairs with groups named `key` and `value`.
color=red size=XL note
Task 3: Match simple email addresses with groups named `user` and `domain`.
Write to ali@example.com or sara@demo.test, not @nobody