2013-01-22 28 views
0

我用正則表達式匹配任何字符串包含某個詞(如:「黑暗」)正則表達式 - JavaScript的 - 匹配字和不匹配的另一個字

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

如何修改它匹配任何字符串包含「暗;但不包含'藍色'? 我想:

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

沒有運氣

+1

你爲什麼不只是使用'.indexOf()'? – Blender

+0

什麼是'黑暗'?如果它不是一個包含字符串的變量,那麼它應該是一個字符串... – elclanrs

+0

如果你清理了格式並給出了你想要匹配的實際字符串,那將會很好。 – MatthewJ

回答

1
if(features[i].attributes.color.indexOf("dark") != -1 && features[i].attributes.color.indexOf("blue") == -1){ 
// found dark, didn't find blue 
} 
0

用途如下:

new RegExp(/dark/) 

U可以使用

.indexOf("dark") 

如果您使用jquery然後

.contains("dark") 
+1

downvote的任何理由??? – Sandeep

+0

是不是'RegExp'接受一個字符串? – elclanrs

+0

我沒有在那裏給一個字符串。它是一種模式。 – Sandeep

2

試試這個正則表達式 「包含黑暗和不藍」:

/^(?!.*\bblue\b)(?=.*\bdark\b).*$/