簡體   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