最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 8浏览 0评论

确定字符串是否从数组中至少包含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

确定字符串是否从数组中至少包含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. 暂无评论