Regular Expression For Duplicate Words

后端 未结 13 1980
终归单人心
终归单人心 2020-11-22 11:13

I\'m a regular expression newbie, and I can\'t quite figure out how to write a single regular expression that would "match" any duplicate consecutive words such as

13条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 11:56

    This expression (inspired from Mike, above) seems to catch all duplicates, triplicates, etc, including the ones at the end of the string, which most of the others don't:

    /(^|\s+)(\S+)(($|\s+)\2)+/g, "$1$2")
    

    I know the question asked to match duplicates only, but a triplicate is just 2 duplicates next to each other :)

    First, I put (^|\s+) to make sure it starts with a full word, otherwise "child's steak" would go to "child'steak" (the "s"'s would match). Then, it matches all full words ((\b\S+\b)), followed by an end of string ($) or a number of spaces (\s+), the whole repeated more than once.

    I tried it like this and it worked well:

    var s = "here here here     here is ahi-ahi ahi-ahi ahi-ahi joe's joe's joe's joe's joe's the result result     result";
    print( s.replace( /(\b\S+\b)(($|\s+)\1)+/g, "$1"))         
    --> here is ahi-ahi joe's the result
    

提交回复
热议问题