繁体   English   中英

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

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

我使用正则表达式匹配包含特定单词的任何字符串(例如:“ dark”)

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

如何修改它以匹配任何包含'dark;的字符串? 但不包含“蓝色”? 我试过了:

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

没有运气

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

/^(?!.*\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
}

用途如下:

new RegExp(/dark/)

你可以用

.indexOf("dark")

如果您使用jQuery

.contains("dark")

暂无
暂无

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

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