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

确定字符串是否从数组中至少包含2个相同元素

运维笔记admin20浏览0评论

确定字符串是否从数组中至少包含2个相同元素

确定字符串是否从数组中至少包含2个相同元素

我想确定字符串是否从数组中至少有两个相同的元素

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个。

回答如下:

您可以使用Array#someArray#some来做到这一点:

String#split
发布评论

评论列表(0)

  1. 暂无评论