Backreferences
Refer back to previously captured groups within the same pattern to match repeated or mirrored text.· 8 min
Concept
A backreference lets you re-match the exact text that a capturing group already matched. This is different from repeating the group's pattern — a backreference matches the same literal text, not the same pattern.
Numbered backreferences use \1, \2, etc., where the number corresponds to the capturing group's position (counted by opening parentheses, left to right).
Named backreferences use \k<name> to reference a named group. This is the same mechanism, but more readable when patterns have many groups.
Common use cases:
- (\w+)\s+\1 — matches a repeated word like "the the"
- (['"]).*?\1 — matches a string enclosed in matching quotes (single or double)
- <(\w+)>.*?</\1> — matches an opening and closing HTML tag pair
**Important:** Backreferences match the captured text, not the pattern. If group 1 captured "abc", then \1 matches only "abc", not any three-letter string.
**Language support:** Numbered backreferences (\1) work in JavaScript, Python, Java, .NET, PHP, Ruby, and PCRE. Go's RE2 engine does not support backreferences. Named backreferences use \k<name> in JavaScript (ES2018+), .NET, and PCRE. Python uses (?P=name) instead.
/(\w+)\s+\1/gMatches a word immediately followed by itself — detects duplicate words like "the the"
/(['"])(.+?)\1/gMatches text enclosed in matching quotes — \1 ensures the closing quote matches the opening one
/<(\w+)>[^<]*</\1>/gMatches an HTML tag pair where the closing tag matches the opening tag name
Exercise
Write a pattern that detects duplicate words separated by a space (e.g., "the the"). Capture the word and use a backreference to match the repetition.
Your pattern:
Must match
Must not match
Try These Patterns
See these concepts in action with real-world patterns from the library: