Match everything except for specified strings

前端 未结 7 1833
名媛妹妹
名媛妹妹 2020-11-22 14:25

I know that the following regex will match \"red\", \"green\", or \"blue\".

red|green|blue

Is there a straightforward way of making it mat

7条回答
  •  甜味超标
    2020-11-22 14:33

    I had the same question, the solutions proposed were almost working but they had some issue. In the end the regex I used is:

    ^(?!red|green|blue).*
    

    I tested it in Javascript and .NET.

    .* should't be placed inside the negative lookahead like this: ^(?!.*red|green|blue) or it would make the first element behave different from the rest (i.e. "anotherred" wouldn't be matched while "anothergreen" would)

提交回复
热议问题