Regular Expression To Match Duplicate Words

A regular expression that matches duplicate words in a string. This can be useful in preventing duplicate words in your paragraphs.

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

Explain:

  • \b Word boundary. Matches a word boundary position between a word character and a non-word character or position (start/end of a string).
  • () Capturing group #1. Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference.
    • \w Word. Matches any word character (alphanumeric & underscore).
    • + Quantifier. Match 1 or more of the preceding token.
  • \s Whitespace. Matches any whitespace character (spaces, tabs, line breaks).
  • + Quantifier. Match 1 or more of the preceding token.
  • \1 Numeric reference. Matches the results of capture group #1.
  • \b Word boundary. Matches a word boundary position between a word character and a non-word character or position (start/end of a string).

Matches:

  • A regular regular expression
  • A regular expression expression pattern.

Non-matches:

  • A regular expression regular

See Also: