Capturing Groups, Named Groups, and Backreferences
Matching text is only half of what regular expressions do. The other half is pulling pieces out of a match and rearranging them, and that is the job of groups. Parentheses turn a regex from a yes-or-no test into a tool that extracts a date's year, swaps the halves of a name, or finds a word repeated by accident. Follow along in the Regex Tester, which shows each captured group as you build the pattern.
Try the Regex Tester toolTest a regular expression against your text in real time. See every match highlighted, with capture groups and flags. Runs entirely in your browser.Parentheses capture
Wrapping part of a pattern in parentheses does two things at once: it groups that part so a quantifier can apply to all of it, and it captures whatever the part matched so you can refer to it afterwards. Captured groups are numbered from left to right by the position of their opening parenthesis, starting at one. Group zero is always the whole match.
pattern: (\d{4})-(\d{2})-(\d{2})
text: 2026-06-30
group 1 = 2026 group 2 = 06 group 3 = 30With those three captures in hand, a replacement string can reorder them. Using $1, $2, $3 to refer to the groups, replacing with $3/$2/$1 turns 2026-06-30 into 30/06/2026. That reorder-on-replace pattern is one of the most useful things regex does.
Non-capturing groups: group without storing
Sometimes you need parentheses only to apply a quantifier, not to capture. A non-capturing group, written with a question mark and colon just inside the parentheses, does exactly that. The pattern (?:ab)+ matches one or more repetitions of ab without filling a capture slot. Using non-capturing groups where you do not need the value keeps your group numbers stable and your intent clear.
Named groups: capture by name
Counting parentheses to find group 3 is fragile. Named groups let you label a capture instead. The syntax puts a name in angle brackets right after the opening parenthesis, and you refer to it by that name rather than a number.
pattern: (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})
replace: $<day>/$<month>/$<year>
2026-06-30 -> 30/06/2026Named groups make a pattern self-documenting and a replacement readable: $<day>/$<month>/$<year> says what it does at a glance, while $3/$2/$1 makes a reader count. When a pattern has more than one or two captures, names are worth the few extra characters.
Backreferences: match the same text again
A backreference matches the exact text a group already captured, not just the same pattern. Inside the regex, a backslash and a number refer back to a numbered group; for named groups the form is a backslash, k, and the name in angle brackets. This is how you find structure that must repeat.
\b(\w+)\s+\1\b finds a doubled word: "the the" matches, "the cat" does not
(?<q>["']).*?\k<q> matches text in quotes, where the closing quote
is the same kind as the opening oneCapture, name, and replace in one place
The Regex Tester lists every numbered and named group it finds and previews a replacement as you edit it, so you can see captures and substitutions update together without leaving your browser. For the precise rules on group syntax and named captures, MDN's reference on groups and backreferences is the authority.
Build a pattern with groupsTest a regular expression against your text in real time. See every match highlighted, with capture groups and flags. Runs entirely in your browser.Related articles
Regex Basics: Characters, Classes, Anchors, and Quantifiers
The core building blocks of regular expressions: literal characters, character classes, anchors, quantifiers, and the flags that change everything.
Lookahead and Lookbehind: Matching by Context
Lookarounds check what surrounds a position without consuming it. Learn positive and negative lookahead and lookbehind with practical examples.
Greedy, Lazy, and Catastrophic Backtracking
Why quantifiers grab as much as they can, how lazy matching reins them in, and how nested quantifiers cause the ReDoS slowdown to avoid.