简体   繁体   English

RegEx-JavaScript-匹配单词而不匹配另一个单词

[英]RegEx - JavaScript - match word and not match another word

I use regex to match any string contains certain word (eg: 'dark') 我使用正则表达式匹配包含特定单词的任何字符串(例如:“ dark”)

     if (features[i].attributes.color.match(new RegExp(dark, "ig")) ) )
..
..

How can I modify it to match any string contains 'dark; 如何修改它以匹配任何包含'dark;的字符串? but NOT contains 'blue' ? 但不包含“蓝色”? I tried: 我试过了:

if(
features[i].attributes.color.match(new RegExp(dark, "ig")) ) && !features[i].attributes.color.match(new RegExp(blue, "ig")) )
}
..
..

with no luck 没有运气

尝试将此正则表达式表示为“包含深色而不是蓝色”:

/^(?!.*\bblue\b)(?=.*\bdark\b).*$/
if(features[i].attributes.color.indexOf("dark") != -1 && features[i].attributes.color.indexOf("blue") == -1){
// found dark, didn't find blue
}

Use as following: 用途如下:

new RegExp(/dark/)

U can use 你可以用

.indexOf("dark")

If you use jquery then 如果您使用jQuery

.contains("dark")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM