Try this regex that can catch 2 or more duplicates words and only leave behind one single word. And the duplicate words need not even be consecutive.
/\b(\w+)\b(?=.*?\b\1\b)/ig
Here, \b
is used for Word Boundary, ?=
is used for positive lookahead, and \1
is used for back-referencing.