Regular Expression For Duplicate Words

后端 未结 13 1975
终归单人心
终归单人心 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:59

    The example in Javascript: The Good Parts can be adapted to do this:

    var doubled_words = /([A-Za-z\u00C0-\u1FFF\u2800-\uFFFD]+)\s+\1(?:\s|$)/gi;
    

    \b uses \w for word boundaries, where \w is equivalent to [0-9A-Z_a-z]. If you don't mind that limitation, the accepted answer is fine.

提交回复
热议问题