在字符串Jquery / Javascript中查找多个值

时间:2011-01-10 15:34:35

标签: javascript jquery string

我有三个类别的字符串

  1. “的SharePoint,天青,IT”;
  2. “的BizTalk,财经”;
  3. “的SharePoint,财经”;
  4. 我需要找到一种方法来检查字符串是否包含例如“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/#

2 个答案:

答案 0 :(得分:4)

试试这个:

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")==truedoesExist("SharePoint,Azure,IT", "SharePoint,BizTalk")==false

答案 1 :(得分:0)

你可以尝试类似的东西:

if(filterArray.indexOf("SharePoint") > -1 && filterArray.indexOf("IT") > -1) {
...
}