Advertisement

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.

The pattern
\b(\w+)\s+\1\b
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
the the quick brown foxMatchthe classic catch — group 1 captures the, the backreference finds it again right after the spaceTry →
one two two three three threeMatchseveral repeats in one string — the first pair (two two) is enough for a matchTry →
hello hello worldMatchany word works — group 1 and its echo are the same text by constructionTry →
a a b bMatchsingle letters count too: group 1 = a, \1 matches the second aTry →
car cargoNo matchthe closing \b rejects a prefix: car matches the start of cargo, but no word edge followsTry →
The the quick brown foxNo matchcase-sensitive by default — The and the are different text, so \1 has nothing to echoTry →
one two oneNo matchthe repeats sit on both sides of another word — the pattern only bridges whitespaceTry →
the cat and the dogNo matchthe word repeats but not side by side — adjacency is the whole scope of this patternTry →
yeah, yeahNo matchthe comma is not whitespace, so \s+ never reaches the second yeahTry →
no repeats in this sentenceNo matchevery word appears once — nothing for the backreference to echoTry →

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

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

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.

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.