确定字符串是否具有至少两个与数组相同的元素
确定字符串是否至少具有来自数组的两个相同元素
const array = ["!", "?"];
const string1 = "!hello"; // should return false
const string2 = "!hello?"; // should return false
const string3 = "!hello!"; // should return true
const string4 = "hello ??"; // should return true
const string5 = "hello ?test? foo"; // should return true
const string6 = "hello ?test ?? foo"; // should return true
我不确定正则表达式或函数会更好吗?
我尝试过:
const array = ["!", "?"];
const string = "test!";
array.every(ar => !string.includes(ar));
但是它仅检测数组中是否有至少1个元素而不是2。
回答如下:使用循环并包括检查该值是否存在
const array = ["!", "?"];
const string1 = "!hello";
var flag=true;
function ch(s)
{
for(let i=0;i<array.length;i++)
{if(!s.includes(array[i]))
{
flag=false;
break;
}
}
console.log(flag)
}
ch(string1)