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:
\bWord 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.\wWord. Matches any word character (alphanumeric & underscore).+Quantifier. Match 1 or more of the preceding token.
\sWhitespace. Matches any whitespace character (spaces, tabs, line breaks).+Quantifier. Match 1 or more of the preceding token.\1Numeric reference. Matches the results of capture group #1.\bWord 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:
- Regex To Match One Of Two Or More Words In A String
- Regex To Match A String Containing Two Words
- Regex To Match Anything But One Or More Words