const arr = ['你好', '测试', '两个词']
const string = '这是两个测试词'
我试图实现的是检查arr
中的这两个单词“两个单词”是否返回到字符串中,即使它们不在彼此后面。也可以按任意顺序,但两个单词都必须存在。
我怎样才能实现这一目标?
最佳答案
const arr = ['hello', 'test', 'two words']
const string = 'this is two test words'
let regexpedArray = arr
.map(w => new RegExp(w.replace(' ', '.*')))
.forEach((val, i) => console.log(`${val.test(string)} for the value ${arr[i]}`))
结果如下:
"false for the value hello"
"true for the value test"
"true for the value two words"
关于javascript - typescript 或 JavaScript : searching for multiple words,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52006799/