【发布时间】:2019-10-01 10:52:56
【问题描述】:
我想确定字符串是否至少有 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 个。
【问题讨论】:
-
数组中是否只有两个元素可以匹配,还是三个或更多?
-
还可以更多
标签: javascript node.js regex function