简体   繁体   中英

RegEx - JavaScript - match word and not match another word

I use regex to match any string contains certain word (eg: 'dark')

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

How can I modify it to match any string contains '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

.contains("dark")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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