Skip to main content
Data & Formatting · In-Browser Regular Expression Lab

Duplicate Words Regex — Find and Remove Consecutive Repeated Words

Finds repeated words separated by whitespace like 'the the' or 'is is' using backreferences.

Flags:
//gi
Match Found
Load Sample:
Match Inspection & Details1 match
Match #1:the the

Test Samples Verification Matrix

Expected Valid Samples (Should Match)

the the company Pass
this is is great Pass
we we went Pass

Expected Invalid Samples (Must Reject)

the company Rejected (Pass)
this is great Rejected (Pass)
very good Rejected (Pass)
Code Implementation Generator
// JavaScript / TypeScript (ES6+)
const pattern = /\b(\w+)\s+\1\b/gi;
const text = "the the company";

const isMatch = pattern.test(text);
console.log("Is Match:", isMatch);

if (isMatch) {
  const matches = text.match(pattern);
  console.log("Matches:", matches);
}

Regular Expression Token Breakdown

Captures a word with `(\w+)` and checks if it is immediately followed by the exact same word via `\1`. Each component of the regular expression serves a distinct syntactical purpose as mapped below:

Token PatternName / ComponentFunction & Rule
\bWord boundaryPrevents partial word matches.
(\w+)Word GroupCaptures word characters into group 1.
\s+WhitespaceOne or more spaces or linebreaks.
\1BackreferenceMatches the exact contents of group 1.

Frequently Asked Questions

How to replace with a single instance?
Use `str.replace(/\b(\w+)\s+\1\b/gi, '$1')`.
Browse All 40 Patterns