DD
DevDash

Regex for Repeated Words

Regex Pattern

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

Detects consecutive duplicate words (e.g. "the the")

Quick Answer

The regex pattern for repeated words is `\b(\w+)\s+\1\b`. Detects consecutive duplicate words (e.g. "the the"). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
the the quick fox✓ Matches
is is broken✓ Matches
very very good✓ Matches
the quick fox✗ No match
no repeats here✗ No match
their there✗ No match

Code Examples

javascript

const regex = /\b(\w+)\s+\1\b/;
const isValid = regex.test(value);

python

import re
pattern = r'\b(\w+)\s+\1\b'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /\b(\w+)\s+\1\b/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/\b(\w+)\s+\1\b/', $value)) {
    echo "valid";
}

java

String pattern = "\\b(\\w+)\\s+\\1\\b";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.