【发布时间】:2021-07-21 11:26:36
【问题描述】:
假设我有两个数组:
const rejectBill = ['cancel', 'delete', 'discard', 'erroneously']
const readBill = ['bill','account','amount']
我有一个字符串,
const stringBill = 'Read bill from text to be cancelled'
我想检查 bill 是否包含 readBill 中的单词然后返回 true,但如果它还包含 rejectBill 中的单词则返回 false。
为此我尝试过:
let stringSatus = readBill.some(keyword => stringBill.toLowerCase().includes(keyword.toLowerCase())) &&
!rejectBill.some(keyword => stringBill.toLowerCase().includes(keyword.toLowerCase()))
但在 stringSatus 的控制台上,它给出了 false。
当字符串包含来自 readBill 数组的单词不包含来自rejectBill 数组的单词时,我希望stringStatus 为真,当字符串包含来自rejectBill 的单词时,我希望stringStatus 为假。
如果有人需要更多信息,请告诉我。
【问题讨论】:
-
stringBill具有来自readBill的'bill'和来自rejectBill的'cancel',因此条件返回false。 -
您想检查全字匹配还是没有注意到字符串中的
cancelled?如果您想将cancel和cancelled视为不同的词,则必须使用 word boundaries 和正则表达式 -
如果你想检查准确的单词匹配,你可以做
const words = new Set(stringBill.toLowerCase().split(' ')); readBill.some(word => words.has(word.toLowerCase())) && !rejectBill.some(word => words.has(word.toLowerCase()))。
标签: javascript node.js arrays string