Say I have a string, like:
where is mummy where is daddy
I want to replace any set of repeating substrings with empty strings - so in this case the where
and is
elements would be removed and the resulting string would be:
mummy daddy
I was wondering if there was any single regex that could achieve this. The regex I tried (which doesn't work) looked like the following:
/(\w+)(?=.*)\1/gi
Where the first capture group is any set of word characters, the second is a positive look ahead to any set of characters (in order to prevent those characters from being included in the result) and then the \1
is a backreference to the first matched substring.
Any help would be great. Thanks in advance!