DevLab
Regex

Named Capture Groups in Regex: Clean Extractions

Learn how named capture groups make regex results readable, how to access them in JavaScript, and why they beat numbered groups for complex patterns.

The Problem with Numbered Groups

Regular capture groups use parentheses and are accessed by index: match[1], match[2]. With five or more groups, knowing which index corresponds to which data requires counting parentheses — a maintenance burden and a frequent source of bugs when the regex is modified.

const dateRegex = /(d{4})-(d{2})-(d{2})/;
const m = "2026-06-13".match(dateRegex);
console.log(m[1]); // year — must remember this is index 1

Named Capture Groups Syntax

Named capture groups use (?<name>...) and populate a groups object on the match result.

const dateRegex = /(?<year>d{4})-(?<month>d{2})-(?<day>d{2})/;
const { year, month, day } = "2026-06-13".match(dateRegex).groups;
// year = "2026", month = "06", day = "13"

Practical Examples

// Parse structured log lines
const logRegex = /(?<ts>S+) (?<level>INFO|WARN|ERROR) (?<msg>.*)/;
const { ts, level, msg } = line.match(logRegex)?.groups ?? {};

// Swap first and last name in a replace
const nameRegex = /(?<first>w+) (?<last>w+)/;
"John Doe".replace(nameRegex, "$<last>, $<first>");
// → "Doe, John"

// With matchAll for multiple matches in a string
const tagRegex = /<(?<tag>w+)>(?<content>[^<]*)</k<tag>>/g;
for (const match of html.matchAll(tagRegex)) {
  console.log(match.groups.tag, match.groups.content);
}

Back-References with \k

// \k<name> references a named group in the same pattern
const xmlTag = /<(?<tag>w+)>.*?</k<tag>>/;
xmlTag.test("<div>content</div>");  // true
xmlTag.test("<div>content</span>"); // false

Browser Support

Named capture groups landed in ES2018: Node.js 10+, Chrome 64+, Firefox 78+, Safari 11.1+. Not in Internet Explorer. For IE support, use numbered groups or compile with Babel.

Practice with these tools

More Learning Topics

RegexRegex Basics: A Complete Beginner's GuideRegexRegex Special Characters: Complete ReferenceRegexRegex Groups and Captures ExplainedRegexRegex Quantifiers: Complete GuideCSSCSS Selectors: The Complete GuideCSSCSS Specificity: Why Your Styles Aren't ApplyingJSONJSONPath Syntax: Query JSON Like XPathTimeUnix Timestamps ExplainedEncodingBase64 Encoding ExplainedEncodingJWT Structure and How It WorksEncodingJWT vs Session Tokens: Which Should You Use?EncodingJWT Refresh Tokens ExplainedCryptoHash Functions Explained: MD5, SHA-256, and When to Use EachEncodingURL Encoding Explained: What %20 Actually MeansJSONJSON Schema Explained: Validate Your JSON DataJSONJSON vs YAML: Which Should You Use?JSONJSON.stringify and JSON.parse: Edge Cases You Should KnowRegexRegex Lookahead and Lookbehind: Match Without ConsumingRegexRegex for Email Validation: The Right ApproachCSSThe CSS Box Model: margin, padding, border, and contentCSSFlexbox vs CSS Grid: When to Use EachCSSCSS Custom Properties (Variables) ExplainedTimeISO 8601 Explained: The Right Way to Format DatesTimeUnix Timestamps vs ISO 8601: Which to Use in Your API?EncodingUTF-8 Explained: How Computers Store TextTextCORS Explained: Why Your API Call is BlockedTextHTTP Status Codes: A Practical Developer GuideColorsColor Spaces Explained: RGB, HSL, HEX, and BeyondColorsColor Contrast for Developers: WCAG Rules and How to CheckNumbersNumber Bases Explained: Binary, Octal, Decimal, and HexNumbersBitwise Operations for Web DevelopersHTMLHTML Semantic Elements: A Complete ReferenceTextText Encoding for Developers: ASCII, UTF-8, and UnicodeRegexWhat is Regex? Complete Guide for DevelopersJSONJSON Format Explained: Structure, Syntax, and Common ErrorsEncodingHow JWT Works: Header, Payload, Signature DecodedHTMLHow to Convert Any Website to Markdown (for LLMs, RAG & Docs)TextPreparing Website Content for RAG: Clean Markdown Pipelines