Text Processingflags: gi
Duplicate Word
Detects consecutive duplicate words using a backreference.
Try it in RegexProPattern
regexJavaScript
/\b(\w+)\s+\1\b/giRaw source: \b(\w+)\s+\1\b
How it works
`\b(\w+)\b` captures a word. `\s+\1\b` matches whitespace followed by the same word (backreference `\1`). Case-insensitive.
Examples
Input
the the cat sat on the the matMatches
the thethe the
Input
no duplicates hereNo match
—Common use cases
- Proofreading
- Content editors
- Writing-quality linters
Related concepts
Word Boundaries: \b and \B
Concept\b matches the position between a word character and a non-word character. It keeps your regex from matching 'cat' inside 'concatenate.'
How to Replace Text Using Regex in JavaScript
How-toString.replace and String.replaceAll accept a regex. Use the g flag for multi-match replace, $1/$2 for capture references, and a function for complex logic.