Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何检查字符串是否包含数组中的某些单词?_Javascript_Node.js_Arrays_String - Fatal编程技术网

Javascript 如何检查字符串是否包含数组中的某些单词?

Javascript 如何检查字符串是否包含数组中的某些单词?,javascript,node.js,arrays,string,Javascript,Node.js,Arrays,String,假设我有两个数组: const rejectBill = ['cancel', 'delete', 'discard', 'erroneously'] const readBill = ['bill','account','amount'] 我有一根绳子 const stringBill = 'Read bill from text to be cancelled' 我想检查bill是否包含readBill中的单词,然后返回true,但如果它也包含rejectBill中的单词,则返回fal

假设我有两个数组:

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的控制台上,它给出了错误的结果

当字符串包含readBill数组中的单词时,我希望stringStatus为true;当字符串包含rejectBill数组中的单词时,我希望stringStatus为false;当字符串包含rejectBill数组中的单词时,我希望stringStatus为false


如果有人需要更多信息,请告诉我。

通过快速查看您的代码,它似乎确实可以正常工作。您的示例“从要取消的文本读取账单”返回false,因为它检测到单词“'cancel'led”。如果要从'cancelled'中删除'cancel',它将返回true。如果要检查空格,我会在readBill和rejectBill中的每个值后面添加一个空格,如下所示:

const rejectBill = ['cancel ', 'delete ', 'discard ', 'erroneously '];
const readBill = ['bill ', 'account ', 'amount '];
当stringBill以其中一个单词结尾时,您还需要添加一个故障保护。这可以通过检查字符串的最后一个字stringBill轻松实现。如果您有任何问题,请随时发表评论,我将尽我所能帮助您

使用正则表达式 我根据
字符串[]
动态创建了
regex
,并测试
字符串是否通过
regex

const rejectBill=[“取消”、“删除”、“放弃”、“错误”];
const readBill=['bill','account','amount'];
const stringBill='从要取消的文本读取账单';
var rejectBillRegex=new RegExp(rejectBill.map(t=>`\\b${t}\\b`).join('|'),'i'),
readBillRegex=newregexp(readBill.map(t=>`\\b${t}\\b`)).join('|'),'i');
var stringSatus=readBillRegex.test(stringBill)&&!拒绝BillRegex.测试(stringBill);
console.log(stringSatus)
使用
String#split
Set

您可以使用
string#split
按空格分割字符串,然后使用列表中构造的
,检查分割的数组是否包含
集中存在的任何字符串(使用
数组#一些

注意:它当前区分大小写,您可以使用
toLowerCase()
toUpperCase()
使其不区分大小写

const
拒绝票据=[“取消”、“删除”、“放弃”、“错误”],
readBill=[“账单”、“账户”、“金额”],
stringBill=“从要取消的文本读取票据”,
strInList=(str,lst,set=new set(lst))=>str.split(“”)。有些(=>set.has)),
isRead=strInList(stringBill,readBill)&&!strInList(stringBill、rejectBill);

控制台日志(isRead)
stringBill
具有来自
readBill
'bill'
和来自
rejectBill
'cancel'
,因此条件返回
false
。是否要检查字符串中的
已取消的
?如果要将
cancel
cancelled
视为不同的单词,则必须使用和regex。如果要检查单词的精确匹配,可以执行
const words=new Set(stringBill.toLowerCase().split(“”));readBill.some(word=>words.has(word.toLowerCase())&&!拒绝Bill.some(word=>words.has(word.toLowerCase())
。在关键字中添加空格是一个糟糕的主意。我必须同意你的意见,我应该使用更好的实现。我认为OP刚开始犯了一个错误,没有在“canceled”中看到cancel。尝试使用
“付账或取消”
更新代码。现在试试看