给出以下数据
green-pineapple-bird
red-apple-dog
blue-apple-cat
green-apple-orange-horse
green-apple-mouse
我试图找出如何使(javascript)regexp.test()匹配任何包含单词“apple”(anywhere)但不匹配任何包含单词“orange”(anywhere)的条目。最终的清单将是:
red-apple-dog
blue-apple-cat
green-apple-mouse
为了便于阅读,我在数据中加入了破折号。实际数据可能包含破折号,也可能不包含破折号。
如果我试试这个:
/^(?!orange).*(apple).*/gm
使用
https://regex101.com/
它匹配所有行。
使用
JavaScript RegEx excluding certain word/phrase?
为了获得灵感,我试着:
/^(?!.*apple\.(?:orange|butter)).*apple\.\w+.*/gm
如果有区别的话,我用的是Mozilla Rhino 1.7R4。