最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

确定字符串是否具有至少两个与数组相同的元素

运维笔记admin13浏览0评论

确定字符串是否具有至少两个与数组相同的元素

确定字符串是否具有至少两个与数组相同的元素

确定字符串是否至少具有来自数组的两个相同元素

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)
发布评论

评论列表(0)

  1. 暂无评论