JavaScript regex for blacklisting words [duplicate]

耗尽温柔 提交于 2019-12-22 13:56:12

问题


I'm trying to write a regex to blacklist certain words. I'm able to create a whitelist like /^(carrots|onions|corn)$/ but how would I convert that into a blacklist?

Edit: To clarify, I'm matching this blacklist against a whole string. For example "corndog" should be allowed. I want the regex equivalent of blacklistArray.indexOf(word) === -1


回答1:


Use negative lookahead:

^(?!.*(?:carrots|onions|corn))


来源:https://stackoverflow.com/questions/43641351/javascript-regex-for-blacklisting-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!