DevKits

Text

Match duplicate consecutive words

Flags: gi

Find repeated words like 'the the' — a common typo.

Pattern

Regular expression

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

How it works

`\1` is a backreference to the first capture group. This finds any word followed by whitespace and then the same word again. Case-insensitive to catch `The the`.

Matches

  • the the
  • and And
  • very very

Non-matches

  • the cat
  • a b

Common gotchas

Go's built-in regexp package does not support backreferences (RE2 syntax). Use the `regexp2` third-party package for backreferences, or accept the limitation.

Language snippets

JavaScript

const re = /\b(\w+)\s+\1\b/gi;
text.match(re);

Python

re.findall(r"\b(\w+)\s+\1\b", text, re.I)

Go

// import "github.com/dlclark/regexp2" — stdlib regexp lacks backreferences

Java

Pattern.compile("\\b(\\w+)\\s+\\1\\b", Pattern.CASE_INSENSITIVE)

Frequently asked questions

What is the regex for duplicate consecutive words?

The pattern \b(\w+)\s+\1\b (flags: gi) matches duplicate consecutive words. `\1` is a backreference to the first capture group. This finds any word followed by whitespace and then the same word again. Case-insensitive to catch `The the`.

What does this duplicate consecutive words pattern match?

It matches strings like the the, and And, very very, but rejects the cat, a b.

How do I use this pattern in JavaScript, Python, Go, Java?

Ready-to-run snippets are provided for JavaScript, Python, Go, Java. Copy the one for your language from the Language snippets section — each wraps the same core pattern in that language's regex API.

What are common pitfalls with a duplicate consecutive words regex?

Go's built-in regexp package does not support backreferences (RE2 syntax). Use the `regexp2` third-party package for backreferences, or accept the limitation.

Related patterns

Try this pattern in a tool

Try any pattern live in the Regex Tester