我正在嘗試檢查數組中是否有匹配PURE JAVASCRIPT。我不知道如何做到這一點,我會很感激你的幫助。檢查數組中的值
var sites = new Array ("site1.com", "site2.com", "site3.com" ...);
// Sites array contains 100 values
var imgs = document.getElementsByTagName("img");
for (var i = 0; i < imgs.length; i++) {
img = imgs[i].src;
// I'm trying to check if is in array,
// and don't waste a lot of size in code
if(img.match(sites)){
notHere(imgs[i]);
}
// This is the working way.
// Even if location is a.site1.com/b/, it will match
if (img.match("site1.com")) {
heReload(imgs[i]);
}
// Repeat this piece of code 100 times
}
}
注意:我不想來檢查的準確值。我想模擬match()
函數,所以如果img =「http://a.b.c/d/」,並且在數組中是「b.c /」,它將執行function()
。
謝謝,這個工作,當你轉換喲正則表達式:d ** – Luis
不要忘了逃跑的特殊字符正則表達式* *點不是唯一的特殊字符。 '.replace(/ [[^ $。|?* +(){} \\]/g,'\\ $ &');' –