Every writer has shipped the doubled word. You type "the the" in a first draft, read the sentence back three times, and see it as correct every time, because your brain reads the word it expects and quietly deletes the spare. Spellcheckers often miss it too, since both copies are spelled perfectly. What catches it reliably is a pattern that compares neighbouring words to each other instead of to a dictionary.

This guide builds that pattern in the free Regex Tester on Ovatly, shows how the match list and its indexes turn a long draft into a short fix list, and is honest about the pairs the pattern cannot see.

Contents

  • Quick answer
  • How the pattern works
  • How to run it, step by step
  • Worked example: three pairs in one sentence set
  • Case handling, and why the i flag matters
  • Using the index to fix a long draft
  • Limitations
  • Common mistakes
  • FAQs
  • Conclusion

Quick answer

Use the pattern \b(\w+)\s+\1\b with the flags g and i. The group captures a word, \s+ crosses the whitespace after it, and the backreference \1 requires the same word again. Open the Regex Tester, type the pattern without slashes in the Pattern field, put gi in the Flags field, paste your draft, and read the match list: every doubled pair appears with its index, so you can jump to each one in your document and delete the spare copy.

How the pattern works

Four pieces do the work. \b anchors the start at a word boundary, so the pattern begins at the start of a word rather than halfway through one. (\w+) captures one whole word, letters, digits and underscores, into group 1. \s+ matches the gap between the two copies: one or more whitespace characters, which covers a plain space, several spaces, a tab, and even a line break, so a word repeated across the end of a line is still caught. Finally \1\b requires the captured word to appear again, ending at a word boundary.

The backreference is the piece that makes this a duplicate finder rather than a word finder. A second \w+ would match any word after any word, which is the whole document. \1 narrows it to the same word the group just captured, whatever that word happens to be. The tester also supports named groups, written (?<name>...), whose values appear in each match's details, but the numbered form is all this pattern needs.

How to run it, step by step

  1. Open the Regex Tester.
  2. Type the pattern \b(\w+)\s+\1\b in the Pattern field, without slashes.
  3. Put gi in the Flags field. The g flag makes the tester list every match; the i flag makes matching, including the backreference, ignore case.
  4. Paste or type the text you want to sweep.
  5. Read the match list: each match shows its text, its index in the text, and its capture groups. Work through the list and fix each pair in your draft.

Worked example: three pairs in one sentence set

Pattern: \b(\w+)\s+\1\b with flags gi. Test text:

I read the the report twice. It was was clear, and And the ending worked.

The tester reports three matches: the the at index 7, was was at index 32, and and And at index 47, each with group 1 holding the repeated word. Two of the pairs are the classic same-case accident. The third is the interesting one: the word "and" ends one clause and "And" begins the next thought after the comma splice, and only the i flag lets the backreference treat them as the same word. Run the same text with g alone and the tester reports just the first two pairs. The capital A hides the third completely.

Case handling, and why the i flag matters

Doubled words often sit at sentence and clause boundaries, which is exactly where capitalisation changes. A writer ends a sentence with "the" in mind, starts the next with "The", or joins two clauses the way the worked example does. A backreference compares text literally by default, so without i those pairs pass unseen. With i, the comparison ignores case everywhere in the pattern, and the pair is reported with the text as written, so you see the capital in the match list and can fix the sentence properly rather than just deleting a word.

The trade-off is small: case-insensitive matching cannot distinguish a genuine pair from two different words that differ only by case, which in practice means a proper name next to its lowercase homophone. In ordinary prose that is rare, and the match list makes it a two-second judgement call.

Using the index to fix a long draft

The match list is what makes this practical on a real document rather than a sample sentence. Each match carries its index, its position in the pasted text, so instead of rereading forty pages you get a short list of locations: a pair at 7, another at 32, another at 47, and so on. Search your document for the surrounding words, delete the spare copy, and move to the next index. The tester reports up to 500 matches, which is far beyond what any draft needs, and because the run happens live as you type, you can re-paste an edited section and watch its pair disappear from the list to confirm the fix.

Limitations

The pattern only sees adjacent duplicates. Two conditions hide a pair entirely. First, punctuation between the copies: against the text Reach the end. End of draft, word, word again, the tester reports no matches at all, because after "end" comes a full stop and after the first "word" a comma, and neither is the whitespace \s+ expects. A doubled word split by a sentence break is one of the most common forms of this typo, and this pattern misses it. Second, distance: a word repeated a sentence later is not a duplicate this pattern can find.

It also flags some repetition that is correct: place names built from a repeated word, dialogue where a character stammers on purpose, poetry, and emphasis. And it works one word at a time; a repeated phrase of two or three words needs a longer group, built the same way but capturing more before the backreference.

Common mistakes

  • Typing the pattern with slashes. The Pattern field takes the pattern body only; flags go in their own field.
  • Running without the g flag. The tester then reports the first pair only, and the draft looks cleaner than it is.
  • Running without the i flag. Pairs split by a capital letter, common at clause boundaries, are silently missed.
  • Trusting a clean result. Zero matches means no adjacent pairs separated by plain whitespace. It says nothing about punctuation-split pairs, which the Limitations section shows escaping completely.
  • Deleting every match blindly. Some repetition is intentional. Read each flagged sentence before removing a word.

FAQs

What does the \1 in the pattern mean? It is a backreference to the first capture group. Whatever (\w+) captured as the first word, \1 requires exactly that same text again after the whitespace. That is what turns a generic word pattern into a duplicate finder: the second word must equal the first, not just be any word.

Why does the pattern miss a pair like And and at the start of a sentence? It does not, as long as the i flag is set. Without i, the backreference compares case-sensitively and a capitalised second word fails. With i, the comparison ignores case and the pair matches. If you only ran with g, add i in the Flags field and run again.

Why did the tester report only one pair? Without the g flag, only the first match is reported, matching how most code uses a pattern. Put g in the Flags field and the tester lists every match with its text, index and capture groups, up to its cap of 500 matches.

Can the pattern find a word repeated later in the same paragraph? No. It only finds two copies that sit next to each other with nothing but whitespace between them. A word repeated three sentences later is invisible to it, and so is a pair separated by a comma or a full stop, because the punctuation breaks the \s+ the pattern expects.

Will it flag repetition that is correct? Yes, sometimes. Names like Pago Pago, quoted speech, and lines of poetry can repeat a word on purpose, and the pattern has no way to know. Treat every match as a place to look, not an instruction to delete: most pairs in ordinary prose are accidents, but the judgement stays yours.

Conclusion

A doubled word survives proofreading because reading is prediction, and a regex does not predict, it compares. One capture group and one backreference, run with gi in the Regex Tester, turn the sweep into a list of indexes you can fix in minutes, as long as you remember it only covers the adjacent, whitespace-separated cases. The same tester handles the opposite kind of condition, requiring or forbidding text around a match, in our guide to using regex lookaheads and lookbehinds. For more patterns and tools like this one, see the Developer Tools archive.