问题
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