Skip to main content
Regexflux
Lesson 13 of 150 completed

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/g

Matches a word immediately followed by itself — detects duplicate words like "the the"

the the cat sat
no no no repeats
hello world
is is this a test
/(['"])(.+?)\1/g

Matches text enclosed in matching quotes — \1 ensures the closing quote matches the opening one

say 'hello' and "goodbye"
'mismatched"
"double" and 'single'
no quotes here
/<(\w+)>[^<]*</\1>/g

Matches an HTML tag pair where the closing tag matches the opening tag name

<b>bold</b> text
<p>paragraph</p>
<b>mismatched</i>
<div>content</div>

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

the the dog ran
it was was cold
Paris in the the spring
is is this correct

Must not match

the cat ran fast
hello world
no duplicates here
this that other

Try These Patterns

See these concepts in action with real-world patterns from the library: