我需要检查一个字符串是否含有三个子串中的任意一个,如果是,就要实现一个函数。我知道可以使用
谢谢!
if (str.indexOf("term1") >= 0)
来检查一个子串,但是有没有一种方法可以检查多个子串,而不用多次使用这段代码呢?谢谢!
if (str.indexOf("term1") >= 0)
来检查一个子串,但是有没有一种方法可以检查多个子串,而不用多次使用这段代码呢?if (/term1|term2|term3/.test("your string")) {
//youre code
}
这可以动态而优雅地实现您所尝试做的事情。
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)))
您可以使用循环。甚至可以创建一个辅助函数,如下所示:
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
}
.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
.map()
文档:
if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0)
{
//your code
}
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
...虽然我认为这可能会容易被重新定义? - musefanarguments
的隐藏参数,它是一个类似数组的结构,包含传递给该方法的所有参数。这就像 Java 中的可变参数。 - Arun P Johny如果你想检查多个字符串匹配并将它们标记出来,这段代码片段可以实现。
function highlightMatch(text, matchString) {
let textArr = text.split(' ');
let returnArr = [];
for(let i=0; i<textArr.length; i++) {
let subStrMatch = textArr[i].toLowerCase().indexOf(matchString.toLowerCase());
if(subStrMatch !== -1) {
let subStr = textArr[i].split('');
let subStrReturn = [];
for(let j=0 ;j<subStr.length; j++) {
if(j === subStrMatch) {
subStrReturn.push('<strong>' + subStr[j]);
} else if (j === subStrMatch + (matchString.length-1)){
subStrReturn.push(subStr[j] + '<strong>');
} else {
subStrReturn.push(subStr[j]);
}
}
returnArr.push(subStrReturn.join(''));
} else {
returnArr.push(textArr[i]);
}
}
return returnArr;
}
highlightMatch('Multi Test returns multiple results', 'multi');
=> (5) ['<strong>Multi<strong>', 'Test', 'returns', '<strong>multi<strong>ple', 'results']