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


当前回答

convert_to_array = function (sentence) {
     return sentence.trim().split(" ");
};

let ages = convert_to_array ("I'm a programmer in javascript writing script");

function confirmEnding(string) {
let target = "ipt";
    return  (string.substr(-target.length) === target) ? true : false;
}

function mySearchResult() {
return ages.filter(confirmEnding);
}

mySearchResult();

您可以像这样检查并使用过滤器返回匹配单词的数组

其他回答

convert_to_array = function (sentence) {
     return sentence.trim().split(" ");
};

let ages = convert_to_array ("I'm a programmer in javascript writing script");

function confirmEnding(string) {
let target = "ipt";
    return  (string.substr(-target.length) === target) ? true : false;
}

function mySearchResult() {
return ages.filter(confirmEnding);
}

mySearchResult();

您可以像这样检查并使用过滤器返回匹配单词的数组

var yourstring = 'tasty food'; // the string to check against


var substrings = ['foo','bar'],
    length = substrings.length;
while(length--) {
   if (yourstring.indexOf(substrings[length])!=-1) {
       // one of the substrings is in yourstring
   }
}
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]
    }
}

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

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

let obj = [{name : 'amit'},{name : 'arti'},{name : 'sumit'}];
let input = 'it';

使用滤镜:

obj.filter((n)=> n.name.trim().toLowerCase().includes(input.trim().toLowerCase()))