🔍Regex Recipes

Detect Repeated Words

Find accidentally repeated words like "the the" in text.

Pattern

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

Explanation

Captures a word and matches if it immediately repeats. Useful for proofreading.

Examples

Repeated word
Input
the the quick brown
Output
✓ Match: "the the"
Different case
Input
The the quick
Output
✓ Match (case-insensitive)
No repetition
Input
the quick brown
Output
✗ No match
Non-adjacent
Input
the quick the
Output
✗ No match

Code Examples

JavaScript
const text = 'I think think this is is a test';
const repeated = text.match(/\b(\w+)\s+\1\b/gi);
// Result: ['think think', 'is is']

// Find and highlight
function highlightRepeated(text) {
  return text.replace(
    /\b(\w+)\s+\1\b/gi,
    '<mark>$&</mark>'
  );
}

Try it Now

💡 Tips

  • Use case-insensitive flag (i) for better detection
  • Backreference \1 refers to first captured group
  • Consider word boundaries (\b) to avoid partial matches
  • Useful for automated proofreading

⚠️ Common Pitfalls

  • Case-sensitive by default (use i flag)
  • Only detects adjacent repetitions
  • Some repetitions are intentional
  • Does not detect similar words (their/there)