Backreferences
A backreference matches the same text that an earlier group captured. \1 means "whatever group 1 matched, again"; \k<name> does the same for a named group. This lets a pattern say "the same thing twice" — something no character class can express.
| Syntax | Refers to |
|---|---|
\1, \2, … |
the text captured by group 1, 2, … |
\k<name> |
the text captured by the named group (JavaScript, Java, .NET) |
(?P=name) |
the same, in Python |
Doubled words
A classic proofreading pattern: a word, whitespace, and the same word again.
/\b(\w+)\s+\1\b/gi
This is is a test of the the checker. Then then we stop.3 matches
#1 "is is" at index 5
group 1: "is"
#2 "the the" at index 21
group 1: "the"
#3 "Then then" at index 38
group 1: "Then"
With the i flag the backreference also compares case-insensitively, so Then then counts as a repeat.
Matching quotes
Strings can use single or double quotes, but the closing quote must match the opening one. Capture the opener and refer back to it:
/(["'])(.*?)\1/g
say "hi" or 'hello' but not "oops'2 matches
#1 "\"hi\"" at index 4
group 1: "\""
group 2: "hi"
#2 "'hello'" at index 12
group 1: "'"
group 2: "hello"
"oops' is rejected because ' is not the same text as the captured ".
Named backreferences
\k<name> makes the intent clearer, especially in longer patterns:
/<(?<tag>[a-z]+)>.*?<\/\k<tag>>/g
<b>bold</b> <i>italic</b> <em>ok</em>2 matches
#1 "<b>bold</b>" at index 0
group tag: "b"
#2 "<em>ok</em>" at index 26
group tag: "em"
<i>italic</b> is skipped: the engine captured i, but no </i> follows, so that start position fails. The search moves on and finds <em>ok</em>, whose closing tag repeats the captured name.
Repeated characters
(.)\1 finds any character immediately repeated; (.)\1{2,} finds runs of three or more:
/(.)\1{2,}/g
Sooo cooool!!! ok3 matches
#1 "ooo" at index 1
group 1: "o"
#2 "oooo" at index 6
group 1: "o"
#3 "!!!" at index 11
group 1: "!"
In code that's a one-liner to calm down excited text:
const s = "Sooo cooool!!! ok";
console.log(s.replace(/(.)\1{2,}/g, "$1$1"));Soo cool!! ok
Backreference vs replacement reference
Don't mix up the two places where you refer to groups:
| Where | JavaScript | Python |
|---|---|---|
| Inside the pattern | \1, \k<name> |
\1, (?P=name) |
| Inside the replacement | $1, lt;name> |
\1, \g<name> |
import re
text = "go go now, stop stop"
print(re.findall(r"\b(\w+) \1\b", text))
print(re.sub(r"\b(?P<w>\w+) (?P=w)\b", r"\g<w>", text))['go', 'stop']
go now, stop
Note: Backreferences take regex beyond the mathematical idea of a "regular" language, and engines that guarantee linear-time matching — Go's
regexp, Rust'sregex, RE2 — don't support them at all. Check your flavour before relying on them.
Test yourself
What does /(\d)\1/ match in 1223?
122223- Nothing
Which is the named backreference syntax in JavaScript?
lt;name>\k<name>(?P=name)\g<name>
Which engines do NOT support backreferences?
- JavaScript and Python
- Java and .NET
- Go's regexp and Rust's regex
- PCRE and Perl