DevLab
Regex

Regex Groups and Captures Explained

Learn how to use capturing groups, non-capturing groups, and named groups to extract data with regex.

What are Capturing Groups?

Parentheses () in regex create a "capturing group". They do two things: they group part of the pattern together (so you can apply quantifiers to the whole group), and they capture the matched text so you can use it in your code.

Example: (\d{4})-(\d{2})-(\d{2}) matches a date like "2024-01-15" and captures the year, month, and day in separate groups.

Accessing Group Matches

In JavaScript: const m = str.match(/(\d{4})-(\d{2})-(\d{2})/) gives you m[1] = year, m[2] = month, m[3] = day.

Groups are numbered from left to right by their opening parenthesis, starting at 1 (index 0 is the full match).

Non-Capturing Groups

If you want to group without capturing, use (?:...). This is useful for applying a quantifier to a group without caring about the capture: (?:https?://)?example\.com — the https:// part is optional but not captured.

Named Capture Groups

Named groups use the syntax (?<name>...). They work like regular groups but you access matches by name instead of number:

(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})

In JavaScript: m.groups.year, m.groups.month, m.groups.day.

Named groups make patterns self-documenting and much easier to maintain.

Backreferences

You can reference a captured group later in the same pattern using \1, \2, etc. (or \k<name> for named groups). This is useful for matching repeated content:

(['"])(.*?)\1 matches either single or double-quoted strings, ensuring the closing quote matches the opening quote.

Practice with these tools

More Learning Topics

RegexRegex Basics: A Complete Beginner's GuideRegexRegex Special Characters: Complete ReferenceRegexRegex 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 GuideRegexNamed Capture Groups in Regex: Clean ExtractionsColorsColor 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