小编Ple*_*klo的帖子

确定字符串中是否至少有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
Run Code Online (Sandbox Code Playgroud)

我不确定会更好的是:正则表达式还是函数?任何都可以。

我尝试了这个:

const array = ["!", "?"];
const string = "test!";

array.every(ar => !string.includes(ar));
Run Code Online (Sandbox Code Playgroud)

但是它仅检测数组中是否存在至少1个元素,而不是2个。

javascript regex function node.js

8
推荐指数
1
解决办法
147
查看次数

标签 统计

function ×1

javascript ×1

node.js ×1

regex ×1