多个子字符串的jquery/javascript检查字符串

多个子字符串的jquery/javascript检查字符串,javascript,jquery,string,search,Javascript,Jquery,String,Search,我需要检查一个字符串是否有三个子字符串中的一个子字符串,如果有,则执行一个函数。我知道如果(str.indexOf(“term1”)>=0,我可以使用检查一个子字符串,但是除了使用此代码的多个实例之外,有没有办法检查多个子字符串 TIA可能是这样的: if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) { //your code } 你可以使用一个

我需要检查一个字符串是否有三个子字符串中的一个子字符串,如果有,则执行一个函数。我知道如果(str.indexOf(“term1”)>=0,我可以使用
检查一个子字符串,但是除了使用此代码的多个实例之外,有没有办法检查多个子字符串

TIA

可能是这样的:

if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) 
{
 //your code
}

你可以使用一个循环。甚至可以创建一个助手函数,如下所示:

function ContainsAny(str, items){
    for(var i in items){
        var item = items[i];
        if (str.indexOf(item) > -1){
            return true;
        }

    }
    return false;
}
你可以这样称呼它:

if(ContainsAny(str, ["term1", "term2", "term3"])){
   //do something
}

你可以这样做

function isSubStringPresent(str){
    for(var i = 1; i < arguments.length; i++){
        if(str.indexOf(arguments[i]) > -1){
            return true;
        }
    }

    return false;
}

isSubStringPresent('mystring', 'term1', 'term2', ...)
函数isSubStringPresent(str){
for(var i=1;i-1){
返回true;
}
}
返回false;
}
isSubStringPresent('mystring','term1','term2',…)
可以使用
.map()
函数将术语数组转换为布尔值数组,指示是否找到了每个术语。然后检查是否有布尔值

给定一个
术语数组

const terms = ['term1', 'term2', 'term3'];
terms.map((term) => string.includes(term)).includes(true);       
如果
string
包含任何
术语,则此行代码将返回
true

const terms = ['term1', 'term2', 'term3'];
terms.map((term) => string.includes(term)).includes(true);       
三个例子:

terms.map((term) => 'Got term2 here'.includes(term)).includes(true);       //true
terms.map((term) => 'Not here'.includes(term)).includes(true);             //false
terms.map((term) => 'Got term1 and term3'.includes(term)).includes(true);  //true
或者,如果要将代码包装成可重用的
hasTerm()
函数:

const hasTerm = (string, terms) =>
   terms.map(term => string.includes(term)).includes(true);

hasTerm('Got term2 here', terms);       //true
hasTerm('Not here', terms);             //false
hasTerm('Got term1 and term3', terms);  //true
试试看:

.map()
文档:

注意事项:

  • 这个答案优化了简单性和可读性。如果预计会出现非常大的术语数组,则使用一个回路,一旦找到术语,该回路就会短路
  • 要支持IE,请使用transfile将出现的
    .includes(x)
    替换为
    .indexOf(x)!=-1
    =>
    具有
    功能
    声明

  • 这将动态地、优雅地实现您想要做的事情

    const terms = ["term1", "term2", "term3"]
    const str = "very large string to check for term1, tern2, etc ..."
    
    // check if the string has some of the terms
    const result1 = terms.some(term => str.includes(term))
    
    // check if the string has all the terms
    const result2 = terms.every(term => str.includes(term))
    
    这也使得为子字符串数组过滤字符串数组变得容易

    const terms = ["term1", "term2", "term3"]
    const strings = ["very large string text ....", "another large string text"] 
    
    // filter the strings of the array that contain some of the substrings we're looking for
    const result1 = strings.filter(str => terms.some(term => str.includes(term)))
    
    // filter the strings of the array that contain all the substrings we're looking for
    const result2 = strings.filter(str => terms.every(term => str.includes(term)))
    

    如果(str.indexOf(“term1”)>=0 | | | str.indexOf(“term2”)>=0 | | str.indexOf(“term3”)>=0)
    这有多困难?以前从未见过,使用额外的参数。你能给我指一下文档吗?有趣的是,我以前从来都不知道
    参数
    。。。尽管我认为这很容易被重新定义?每个javascript方法都会得到一个名为
    arguments
    的隐藏参数,它是一个类似数组的结构,所有参数都传递给该方法。另一方面,它就像Java语言中的变量参数,你的代码片段错了吗?如果字符串中没有任何子字符串,则将以false退出。OP难道不想检查字符串中是否至少有一个吗?@Ashburaczenko错过了,我以为是全部三个,对未知数量的术语进行了更改虽然我同意,但他要求了三个子字符串。问题是:“我需要检查字符串是否有三个子字符串之一”不是要求所有人都在场,至少一个人在场。你不必解释反对票,但我猜不是每个人都喜欢正则表达式。在这种情况下,它们是不需要的,有些人可能会说它们降低了代码的可读性。但是它们简单而优雅。如果我知道我的正则表达式,为什么我要使用loong或(| |)子句或循环。答案的解释不见了。为什么你要要求解释否决票?不要要求你没有给出的东西。回答很好!为我工作,并帮助我找到了一个更容易理解的版本,该版本同样有效:str=“your string”;str.match(/(term1 | term2 | term3)/g);但比赛在技术上是劣势的,在这种情况下,当然是另一种形式,这是一个伟大的解决方案。非常感谢。