Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex: Repeated words
Regex pattern · find & extract

Regex: Repeated words

Regex to find duplicate words: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use /\b(\w+)\s+\1\b/gi with the global flag to find every match. In "This is is a test test." it finds "is is", "test test". Backreferences are not supported in Go’s RE2 engine.

Last updated · Maintained by FreeAIToolz · How we test

/\b(\w+)\s+\1\b/gi

3 match(es)

This is is a test test. The the cat sat.
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
\b(\w+)capture a whole word
\s+whitespace
\1\bthe same word again (backreference)
i flagcase-insensitive, so "The the" matches

Flags: i makes it case-insensitive. g finds every match instead of stopping at the first.

Examples

InputMatches
This is is a test test.is is, test test
The the cat sat.The the

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

Backreferences are not supported in Go’s RE2 engine.

When to use this pattern

Proofreading articles and catching typos like “the the”. Replace with $1 to remove the duplicate.

Use it in your language

JavaScript

const matches = text.match(/\b(\w+)\s+\1\b/gi) ?? [];

Python

import re
matches = [m.group(0) for m in re.finditer(r'\b(\w+)\s+\1\b', text, re.IGNORECASE)]

PHP

preg_match_all('~\\b(\\w+)\\s+\\1\\b~i', $text, $m);
$matches = $m[0];

Java

Matcher m = Pattern.compile("\\b(\\w+)\\s+\\1\\b", Pattern.CASE_INSENSITIVE).matcher(text);
while (m.find()) { System.out.println(m.group()); }

Go’s regexp package (RE2) does not support backreferences, so this exact pattern will not compile in Go.

More regex patterns

Frequently asked questions

What is the regex for Repeated words?
/\b(\w+)\s+\1\b/gi. Backreferences are not supported in Go’s RE2 engine.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It does not work in Go, because RE2 has no backreference support.
How do I replace the matches?
In JavaScript use text.replace(/\b(\w+)\s+\1\b/gi, "replacement"); in Python use re.sub(r'\b(\w+)\s+\1\b', "replacement", text).