确定字符串是否从数组中至少包含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#some
和Array#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#some
和Array#some
来做到这一点:
String#split