Pattern library / Strings & Text
Regex to Find Repeated Words
Doubling a word by accident — the the, a a — is one of the oldest text-cleaning questions (regex find repeated words, regex detect duplicate words, regex match same word twice). The pattern has three moving parts: \b(\w+) captures the first whole word, \s+ bridges the whitespace, and the backreference \1 demands the exact same text again — closed by a second \b so only whole words count (car cargo stays a non-match). One caveat up front: \1 is a backreference, so the pattern runs in JavaScript, Python and PCRE, while RE2 rejects it — GA4 and BigQuery need the LAG-based alternative in the BigQuery snippet below.
How the pattern works
- Three parts, one echo:
\b(\w+)captures the first word (boundaries keep partial words out),\s+matches the whitespace run between the pair, and\1— a backreference — re-matches that captured text exactly. The trailing\bis what rejectscar cargo: group 1 matches the first three letters of cargo, but no word edge follows them. - Find mode returns every duplicated pair:
[...text.matchAll(/\b(\w+)\s+\1\b/g)].map(m => m[1])gives['two','three']for the sample in “Where to use it”. Drop thegflag for a simple yes/no — and remember a sharedg-flagged regex keepslastIndexbetween calls, so reset it (the tester does) before the next test. - Matching is case-sensitive:
The theis not a repeat as far as\1is concerned. JavaScript switches with theiflag (/\b(\w+)\s+\1\b/gi), Python withre.I. Note also that\wcovers digits and underscores, solog_1 log_1counts as a repeat — use[A-Za-z]+instead if only letters should count. - The backreference is both the engine and the limitation:
\1is what makes the pattern work in JavaScript, Python, PCRE, Java and .NET — and exactly what RE2, the engine behind GA4, BigQuery, Go and Rust, refuses to parse. Full feature matrix in RE2 vs PCRE.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
the the quick brown fox | Match | the classic catch — group 1 captures the, the backreference finds it again right after the space | Try → |
one two two three three three | Match | several repeats in one string — the first pair (two two) is enough for a match | Try → |
hello hello world | Match | any word works — group 1 and its echo are the same text by construction | Try → |
a a b b | Match | single letters count too: group 1 = a, \1 matches the second a | Try → |
car cargo | No match | the closing \b rejects a prefix: car matches the start of cargo, but no word edge follows | Try → |
The the quick brown fox | No match | case-sensitive by default — The and the are different text, so \1 has nothing to echo | Try → |
one two one | No match | the repeats sit on both sides of another word — the pattern only bridges whitespace | Try → |
the cat and the dog | No match | the word repeats but not side by side — adjacency is the whole scope of this pattern | Try → |
yeah, yeah | No match | the comma is not whitespace, so \s+ never reaches the second yeah | Try → |
no repeats in this sentence | No match | every word appears once — nothing for the backreference to echo | Try → |
Where to use it
JavaScript (collect every duplicated word)
const text = 'one two two three three three'; [...text.matchAll(/\b(\w+)\s+\1\b/g)].map(m => m[1]); // ['two', 'three']
Python (findall → the repeated words)
import re text = 'one two two three three three' re.findall(r'\b(\w+)\s+\1\b', text) # ['two', 'three']
BigQuery SQL (RE2 has no backreference — compare neighbours with LAG)
WITH words AS ( SELECT word, LAG(word) OVER (ORDER BY i) AS prev FROM UNNEST(SPLIT(text, ' ')) AS word WITH OFFSET AS i ) SELECT DISTINCT word FROM words WHERE word = prev; -- 'one two two three three three' → two, three
Common mistakes
- RE2 rejects backreferences, so this exact pattern cannot run in GA4 segments or BigQuery
REGEXP_CONTAINS— it fails to parse, it does not merely match less. GA4 has no repeated-word operator at all; do the comparison in BigQuery with the LAG snippet above, or in your code. Feature matrix: RE2 vs PCRE. - Only adjacent repeats match.
the cat and the dogrepeats “the” but never matches, because\s+cannot jump over the words in between. For a word that recurs anywhere on the line, see the FAQ below — and note that fix is JavaScript/Python-only too. - The bridge is whitespace only.
yeah, yeahfails because\s+stops at the comma; widen the middle to(\w+)[\s,]+\1\b— or(\w+)\W+\1\bfor any non-word separator. Keep the closing\b: drop it andcar cargoturns into a false positive.
FAQ
How do I find every repeated word in a string?
Run the pattern in find mode with the g flag and map group 1: [...text.matchAll(/\b(\w+)\s+\1\b/g)].map(m => m[1]) returns ['two','three'] for one two two three three three. Python equivalent: re.findall(r'\b(\w+)\s+\1\b', text). For a plain yes/no, /\b(\w+)\s+\1\b/.test(text).
How do I match a word repeated anywhere in the line, not just side by side?
Add a lookahead that checks the rest of the line for the captured word: \b(\w+)\b(?=.*\b\1\b) matches the first “the” in “the cat and the dog”. Both features — lookahead and backreference — are JavaScript/Python/PCRE-only; RE2 rejects them, so for large texts or Google tools, split into words and count duplicates in code (a Map) or with the BigQuery LAG snippet above.
Does this pattern work in GA4, BigQuery and Go (RE2)?
No — \1 is a backreference and RE2 does not implement them, so the pattern is rejected outright before it can match anything. JavaScript, Python and PCRE accept it unchanged. In BigQuery, emulate adjacency with LAG(word) over split words as in the snippet above; GA4 offers no repeated-word operator, so filter the BigQuery export instead. Details: RE2 vs PCRE.
Related patterns
Regex to Match Any Word from a List — Test & Copy · Strong Password Regex - Validate Password Strength · Alphanumeric Regex — Letters & Numbers Only
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.