非常直截了当。在javascript中,我需要检查字符串是否包含数组中持有的任何子字符串。
当前回答
Javascript函数使用搜索字符串或搜索字符串数组搜索标签或关键字数组。(使用ES5的一些数组方法和ES6的箭头函数)
// returns true for 1 or more matches, where 'a' is an array and 'b' is a search string or an array of multiple search strings
function contains(a, b) {
// array matches
if (Array.isArray(b)) {
return b.some(x => a.indexOf(x) > -1);
}
// string match
return a.indexOf(b) > -1;
}
使用示例:
var a = ["a","b","c","d","e"];
var b = ["a","b"];
if ( contains(a, b) ) {
// 1 or more matches found
}
其他回答
基于t。j。克劳德的答案
使用转义的RegExp测试至少一个子字符串的“至少一次”出现。
函数buildSearch(substrings) { 返回新的RegExp( 子字符串 . map(函数(s) {s.replace返回 (/[.*+?^${}()|[\]\\]/ g , '\\$&');}) .join('{1,}|') + '{1,}' ); } var pattern = buildSearch(['hello','world']); console.log(模式。测试('你好')); console.log(模式。Test ('what a wonderful world')); console.log(模式。Test ('my name is…'));
Javascript函数使用搜索字符串或搜索字符串数组搜索标签或关键字数组。(使用ES5的一些数组方法和ES6的箭头函数)
// returns true for 1 or more matches, where 'a' is an array and 'b' is a search string or an array of multiple search strings
function contains(a, b) {
// array matches
if (Array.isArray(b)) {
return b.some(x => a.indexOf(x) > -1);
}
// string match
return a.indexOf(b) > -1;
}
使用示例:
var a = ["a","b","c","d","e"];
var b = ["a","b"];
if ( contains(a, b) ) {
// 1 or more matches found
}
const str = '此字符串是否包含下面数组中的一个或多个字符串?'; Const arr = ['one', 'two', 'three']; Const包含= arr。Some (element => { If (str.includes(element)) { 返回true; } 返回错误; }); console.log(包含);/ /正确的
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}"`); }
推荐文章
- 窗口。亲近与自我。close不关闭Chrome中的窗口
- 同步和异步编程(在node.js中)的区别是什么?
- 在d3.js中调整窗口大小时调整svg的大小
- 不区分大小写的列表排序,没有降低结果?
- 如何将两个字符串相加,就好像它们是数字一样?
- 绑定多个事件到一个监听器(没有JQuery)?
- 在JavaScript中将JSON字符串解析为特定对象原型
- 将字符串“true”/“false”转换为布尔值
- 如何使用JavaScript代码获得浏览器宽度?
- event.preventDefault()函数在IE中无法工作
- 如何在Java中将int[]转换为Integer[] ?
- indexOf()和search()的区别是什么?
- 错误:'types'只能在.ts文件中使用- Visual Studio Code使用@ts-check
- React-Native:应用程序未注册错误
- LoDash:从对象属性数组中获取值数组