Regular expression - match all words but match unique words only once

后端 未结 3 1297
不知归路
不知归路 2020-12-16 04:23

Is it possible to use a regular expression to match all words but match unique words only once? I am aware there are other ways of doing this however I\'m interested

相关标签:
3条回答
  • 2020-12-16 05:02

    Exactly as maček's answer, but with an extra \b before the back-reference, otherwise if you had

    glass shoes door window door glasses. window glasses sunglasses

    You'd miss out a match for glasses as it finds it in the word sunglasses.

    /(\w+\b)(?!.*\b\1\b)/

    0 讨论(0)
  • 2020-12-16 05:11

    For search distinct words in multiline text use [\s\S] instead of .

    (\b\w+\b)(?![\s\S]*\b\1\b)
    
    0 讨论(0)
  • 2020-12-16 05:20

    Pretty close, just readd the \b in the negative lookahead

    /(\w+\b)(?!.*\1\b)/
    

    See it on Rubular

    0 讨论(0)
提交回复
热议问题