[英]Finding multiple values in a string Jquery / Javascript
我有三個類別的字符串
我需要找到一種方法來檢查字符串是否包含例如“SharePoint”和“IT”,或“BizTalk”和“Finance”。 測試是單獨的字符串。
我將如何遍歷所有類別字符串(1 - 3)並僅返回具有所有源實例的那些字符串。
我試過以下
function doesExist(source, filterArray)
{
var substr = filterArray.split(" ");
jQuery.each(substr, function() {
var filterTest = this;
if(source.indexOf(filterTest) != -1 )
{
alert("true");
return true;
}else
{
alert("false");
return false;
}
});
}
幾乎沒有成功...上面的代碼一次檢查一個而不是兩個,所以返回的結果是不正確的。 任何幫助都會很棒。
謝謝
克里斯
更新:這是一個正在進行中的工作版本的鏈接..http://www.invisiblewebdesign.co.uk/temp/filter/#
嘗試這個:
function doesExist(source, filter)
{
var sourceArray = source.split(",");
var filterArray = filter.split(",");
var match = true;
$.each(filterArray, function(i, val) {
match = match && ($.inArray(val, sourceArray) != -1);
});
return match;
}
給出了doesExist("SharePoint,Azure,IT", "SharePoint,IT")==true
但是doesExist("SharePoint,Azure,IT", "SharePoint,BizTalk")==false
。
你可以嘗試類似的東西:
if(filterArray.indexOf("SharePoint") > -1 && filterArray.indexOf("IT") > -1) {
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.