【问题标题】:jquery/javascript check string for multiple substringsjquery/javascript 检查多个子字符串的字符串
【发布时间】:2013-02-18 13:31:37
【问题描述】:

我需要检查一个字符串是否具有三个子字符串之一,如果是,则实现一个函数。我知道我可以使用if (str.indexOf("term1") >= 0) 检查一个子字符串,但是有没有办法检查多个子字符串而不是使用此代码的多个实例?

TIA

【问题讨论】:

  • if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) 有多难?

标签: javascript jquery string search


【解决方案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)))

【讨论】:

  • 这是一个很好的解决方案。谢谢。
【解决方案2】:

.map() 函数可用于将术语数组转换为 布尔值 数组,指示是否找到每个术语。然后检查是否有任何 布尔值true

给定一个terms的数组:

const terms = ['term1', 'term2', 'term3'];

如果string 包含terms 中的任何一个,这行代码将返回true

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

试试看:
https://codepen.io/anon/pen/MzKZZQ?editors=0012

.map() 文档:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

注意事项:

  1. 此答案针对简单性和可读性进行了优化。如果需要非常大的术语数组,请使用一旦找到术语就会短路的循环。
  2. 为了支持 IE,转译以用 .indexOf(x) !== -1=> 替换出现的 .includes(x)function 声明。

【讨论】:

    【解决方案3】:

    你可以这样做

    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', ...)
    

    【讨论】:

    • 以前从未见过,使用了额外的参数。你能指出我的文档吗?
    • 有趣,我以前从不知道arguments...虽然我认为这很容易被重新定义?
    • 每个 javascript 方法都有一个名为 arguments 的隐藏参数,它是一个类似数组的结构,所有参数都传递给方法。就像java中的变量参数
    • 另一方面,你是不是 sn-p 错了?如果任何子字符串不在字符串中,您将以 false 退出。 OP 不想检查字符串中是否至少有一个?
    • @AshBurlaczenko 错过了,我以为是三个,做出了改变
    【解决方案4】:
    if (/term1|term2|term3/.test("your string")) {
       //youre code
    }
    

    【讨论】:

    • 您不必解释否决票,但我猜不是每个人都喜欢正则表达式。在这种情况下,它们不是必需的,有些人可能会说它们使代码的可读性降低。
    • 但它们既简单又优雅。如果我知道我的正则表达式,我为什么要使用 loong OR (||) 子句或循环。
    • 缺少答案的解释为什么你会要求解释否决票?不要要求你没有给出的东西。
    • 很好的答案!为我工作并帮助我找到了一个更易于理解的版本:str = "your string"; str.match(/(term1|term2|term3)/g);但是在这种情况下,匹配在技术上当然较差
    【解决方案5】:

    您可以使用循环。甚至可以像这样创建一个辅助函数:

    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
    }
    

    【讨论】:

    • 来自问题:“我需要检查一个字符串是否有三个子字符串之一”这不是 all 的要求目前,只有其中至少一个。
    【解决方案6】:

    也许是这样的:

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

    【讨论】:

    • 虽然对于未知数量的术语来说并不理想
    • 我同意,但他要求 3 个子字符串。
    猜你喜欢
    • 2011-02-18
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 2014-06-25
    • 2013-11-12
    • 1970-01-01
    相关资源
    最近更新 更多