我想确定字符串是否从数组中至少有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个。