One js regex for matching duplicate substrings?

Say I have a line, for example:

where is mummy where is daddy

I want to replace any set of repetitive substrings blank lines - so in this case, the elements whereand iswill be removed, and the resulting string will be:

mummy daddy

I was wondering if there was any one regular expression that could achieve this. The regular expression I tried (which doesn't work) is as follows:

/(\w+)(?=.*)\1/gi

If the first capture group is any set of word characters, the second is a positive look at any set of characters (to prevent the inclusion of these characters in the result), and then it \1is a backward link to the first subscript.

Any help would be great. Thanks in advance!

+4
1

, \w+ , \1 "", .

, , RegExp, ( .. - ) :

var re = /(\b\w+\b)(?=.*\b\1\b)/gi;                  // Get the repeated whole words
var str = 'where is mummy where is daddy';
var patts = str.match(re);                       // Collect the matched repeated words
var res = str.replace(RegExp("\\s*\\b(?:" + patts.join("|") +")\\b", "gi"), ""); //  Build the pattern for replacing all found words
document.body.innerHTML = res;
Hide result

(\b\w+\b)(?=.*\b\1\b):

  • (\b\w+\b) - 1 , [A-Za-z0-9_]
  • (?=.*\b\1\b) - , , 1, - ( ). , [\s\S] . , , \b \w+, \1.

, /\s*\b(?:where|is)\b/gi:

  • \s* - .
  • \b(?:where|is)\b - (?:...|...): where, is ( - /i).
+6

All Articles