How can I invert a regular expression in JavaScript?

后端 未结 4 1962
野趣味
野趣味 2020-11-27 03:46

I have a string A and want to test if another string B is not part of it. This is a very simple regex whose result can be inverted afterwards.

I could do:



        
4条回答
  •  不知归路
    2020-11-27 04:32

    ^(?!.*(word1|word2|word3))
    

    will match a string that does not contain any of word1, word2, or word3 (and you can extend the list indefinitely). But this also matches null strings. To reject nulls use

    ^(?!$)(?!.*(word1|word2|word3))  
    

提交回复
热议问题