非常直截了当。在javascript中,我需要检查字符串是否包含数组中持有的任何子字符串。


当前回答

全面支持(除了@ricca的版本)。

wordsArray = ['hello', 'to', 'nice', 'day'] yourString = '你好。今天天气不错’。 result = wordsArray。每个(w => yourstring .include (w)) console.log(“结果:”,结果)

其他回答

这太迟了,但我刚刚遇到了一个问题。在我自己的项目中,我使用以下方法来检查字符串是否在数组中:

["a","b"].includes('a')     // true
["a","b"].includes('b')     // true
["a","b"].includes('c')     // false

通过这种方式,你可以获取一个预定义数组并检查它是否包含字符串:

var parameters = ['a','b']
parameters.includes('a')    // true
var str = "texttexttext";
var arr = ["asd", "ghj", "xtte"];
for (var i = 0, len = arr.length; i < len; ++i) {
    if (str.indexOf(arr[i]) != -1) {
        // str contains arr[i]
    }
}

编辑: 如果测试的顺序不重要,你可以使用这个(只有一个循环变量):

var str = "texttexttext";
var arr = ["asd", "ghj", "xtte"];
for (var i = arr.length - 1; i >= 0; --i) {
    if (str.indexOf(arr[i]) != -1) {
        // str contains arr[i]
    }
}

我也遇到过这样的问题。我有一个URL,我想检查链接是否以图像格式或其他文件格式结束,有一个图像格式数组。以下是我所做的:

const imagesFormat = ['.jpg','.png','.svg']
const link = "https://res.cloudinary.com/***/content/file_padnar.pdf"
const isIncludes = imagesFormat.some(format => link.includes(format))
    
// false
function containsAny(str, substrings) {
    for (var i = 0; i != substrings.length; i++) {
       var substring = substrings[i];
       if (str.indexOf(substring) != - 1) {
         return substring;
       }
    }
    return null; 
}

var result = containsAny("defg", ["ab", "cd", "ef"]);
console.log("String was found in substring " + result);

没有内置的东西可以帮你做这个,你必须为它写一个函数,尽管它可以只是一个对某些数组方法的回调。

有两种方法:

数组一些方法 正则表达式

数组的一些

数组some方法(在ES5中添加)使这非常简单:

if (substrings.some(function(v) { return str.indexOf(v) >= 0; })) {
    // There's at least one
}

更好的箭头函数和newish includes方法(都是ES2015+):

if (substrings.some(v => str.includes(v))) {
    // There's at least one
}

生活例子:

const substrings = ["one", "two", "three"]; let str; // Setup console.log(`Substrings: ${substrings}`); // Try it where we expect a match str = "this has one"; if (substrings.some(v => str.includes(v))) { console.log(`Match using "${str}"`); } else { console.log(`No match using "${str}"`); } // Try it where we DON'T expect a match str = "this doesn't have any"; if (substrings.some(v => str.includes(v))) { console.log(`Match using "${str}"`); } else { console.log(`No match using "${str}"`); }

正则表达式

如果你知道字符串不包含任何正则表达式中的特殊字符,那么你可以欺骗一下,像这样:

if (new RegExp(substrings.join("|")).test(string)) {
    // At least one match
}

...这将创建一个正则表达式,它是您正在寻找的子字符串的一系列更改(例如,one|two),并测试是否有匹配它们中的任何一个,但如果任何子字符串包含正则表达式中的任何特殊字符(*,[等),您必须首先转义它们,您最好只做无聊的循环。有关如何逃离它们的信息,请参阅这个问题的答案。

生活例子:

const substrings = ["one", "two", "three"]; let str; // Setup console.log(`Substrings: ${substrings}`); // Try it where we expect a match str = "this has one"; if (new RegExp(substrings.join("|")).test(str)) { console.log(`Match using "${str}"`); } else { console.log(`No match using "${str}"`); } // Try it where we DON'T expect a match str = "this doesn't have any"; if (new RegExp(substrings.join("|")).test(str)) { console.log(`Match using "${str}"`); } else { console.log(`No match using "${str}"`); }