Match everything except for specified strings

前端 未结 7 1767
名媛妹妹
名媛妹妹 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 14:34

    Matching Anything but Given Strings

    If you want to match the entire string where you want to match everything but certain strings you can do it like this:

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

    This says, start the match from the beginning of the string where it cannot start and end with red, green, or blue and match anything else to the end of the string.

    You can try it here: https://regex101.com/r/rMbYHz/2

    Note that this only works with regex engines that support a negative lookahead.

提交回复
热议问题