Backreferences \1
A backreference \1 matches the same text that group 1 captured earlier in the pattern: (\w)\1 finds a doubled letter and \b(\w+) \1\b finds a repeated word. In a replacement string use $1 (JavaScript) or \1 (Python); \k<name> refers back to a named group.
Syntax
(x)\1 \k<name> (["']).*?\1Examples
"the the cat sat sat".match(/\b(\w+) \1\b/g)Result: ["the the", "sat sat"]
"bookkeeper".match(/(\w)\1/g)Result: ["oo", "kk", "ee"]
"the the cat".replace(/\b(\w+) \1\b/g, "$1")Result: "the cat"
Collapse the repeated word; the replacement uses $1, not \1.
['"ok"', '"bad\''].map(s => /^(["']).*\1$/.test(s))Result: [true, false]
The closing quote must equal the opening one.
Common mistakes
- Writing \1 in a JavaScript replacement string; it inserts a literal backslash-1. Use $1.
- Referring to a group before it has captured, such as \1(a); the reference is empty or fails.
- Relying on \1 across engines when group 1 did not participate: JavaScript matches the empty string, most other engines fail the match.
- Ambiguity with \10: it means group 10 if that many groups exist, otherwise group 1 followed by a 0.
Practice Backreferences \1
These drills check your answer on live data, free, no account needed.
- Backreference: doubled wordsA match anywhere counts. Match lines where the same whole word appears twice in a row, separated by one space, like the the.
- Matching quote marksThe whole line must match. Match text wrapped in quotes, where the closing quote is the same kind as the opening one: both double or both single.
- Capture a standalone yearCapture the first standalone four-digit number on each line in group 1. Digits that are part of a longer number do not count. Lines without one give no match.
Questions
What is \1 in regex?
A reference to whatever capture group 1 matched, so the same text must appear again at that point. \2 refers to group 2, and so on.
How do I find duplicate words with regex?
/\b(\w+)\s+\1\b/gi finds a word followed by itself, case-insensitively, with any whitespace between (the the, The the).
Can a backreference match across lines?
Yes. The captured text is compared wherever the reference sits; only the dot and anchors care about line breaks. Use \s+ instead of a space to allow newlines between repeats.
Related
- Capture groupsHow parentheses capture text, reading groups in JavaScript via match, matchAll and exec, reusing them with $1 in replace, and the g trap that loses groups.
- Named groups (?<name>...)How to capture by name, read match.groups.name, use $<name> in replacements and \k<name> as a backreference, and the (?P<name>) syntax Python needs.
- Word boundary \bWhat \b matches, how to find or replace whole words only, what \B does, and why \b fails next to punctuation, in string-built regexes and with Unicode text.
- match, test, replace in JavaScriptWhich JavaScript regex method to use for a yes/no check, the first match, all matches with groups, substitution and splitting, with return values and g rules.
Previous: Named groups (?<name>...)Next: Lookahead (?=...) and (?!...)