简体   繁体   English

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

[英]How to check a string includes or doesnot include certain words from array?

Suppose I have two array:假设我有两个数组:

const rejectBill = ['cancel', 'delete', 'discard', 'erroneously']

const readBill = ['bill','account','amount']

And I have an string,我有一个字符串,

const stringBill = 'Read bill from text to be cancelled'

I want to check if bill contains words from readBill then return true but if it also contains word from rejectBill then return false.我想检查 bill 是否包含来自 readBill 的单词然后返回 true,但如果它还包含来自 rejectBill 的单词则返回 false。

For this I tried:为此,我尝试了:

let stringSatus = readBill.some(keyword => stringBill.toLowerCase().includes(keyword.toLowerCase())) && 
                   !rejectBill.some(keyword => stringBill.toLowerCase().includes(keyword.toLowerCase()))

But on console of stringSatus, it gives false.但是在 stringSatus 的控制台上,它给出了错误。

I want stringStatus to be true when string contains words from readBill array doesnot contain word from rejectBill array and false when string contains word from rejectBill.当字符串包含来自 readBill 数组的单词不包含来自rejectBill 数组的单词时,我希望 stringStatus 为真,当字符串包含来自rejectBill 的单词时为假。

If any one needs any more information please let me know.如果有人需要更多信息,请告诉我。

Using Regex使用正则表达式

I have created a regex on the fly based on the string[] and test if the string passes the regex .我根据string[]动态创建了一个regex ,并测试string是否通过了regex

 const rejectBill = ['cancel', 'delete', 'discard', 'erroneously']; const readBill = ['bill', 'account', 'amount']; const stringBill = 'Read bill from text to be cancelled'; var rejectBillRegex = new RegExp(rejectBill.map(t => `\\b${t}\\b`).join('|'), 'i'), readBillRegex = new RegExp(readBill.map(t => `\\b${t}\\b`).join('|'), 'i'); var stringSatus = readBillRegex.test(stringBill) &&.rejectBillRegex;test(stringBill). console.log(stringSatus)

Using String#split and Set .使用String#splitSet


You can split the string by spaces using String#split and then using a Set constructed out of the list check if splited array contains any (using Array#some ) string that is present in the Set .您可以使用String#split按空格拆分字符串,然后使用从列表中构造的Set检查拆分后的数组是否包含Set中存在的任何(使用Array#some )字符串。

NOTE: It is currently case sensitive, you can use toLowerCase() or toUpperCase() to make it case insensitive.注意:它目前区分大小写,您可以使用toLowerCase()toUpperCase()使其不区分大小写。

 const rejectBill = ["cancel", "delete", "discard", "erroneously"], readBill = ["bill", "account", "amount"], stringBill = "Read bill from text to be cancelled", strInList = (str, lst, set = new Set(lst)) => str.split(" ").some((s) => set.has(s)), isRead = strInList(stringBill, readBill) &&,strInList(stringBill; rejectBill). console;log(isRead);

From taking a quick look at your code, it seems like it does work as attended.通过快速查看您的代码,它似乎可以正常工作。 Your example, "Read bill from text to be cancelled," is returning false because it detects the word "'cancel'led."您的示例“从要取消的文本中读取帐单”返回 false,因为它检测到单词“'cancel'led”。 If you were to remove 'cancel' from 'cancelled,' it would return true.如果您要从“取消”中删除“取消”,它将返回 true。 If you are looking to check for spaces, I would add a space after each value in readBill and rejectBill, as so:如果您要检查空格,我会在 readBill 和 rejectBill 中的每个值之后添加一个空格,如下所示:

const rejectBill = ['cancel ', 'delete ', 'discard ', 'erroneously '];
const readBill = ['bill ', 'account ', 'amount '];

You would also need to add a fail-safe though for when stringBill ends in one of those words.当 stringBill 以其中一个单词结尾时,您还需要添加一个故障保护。 That could easily be implemented by checking the last word of the string, stringBill.这可以通过检查字符串的最后一个单词 stringBill 轻松实现。 If you have any questions, feel free to leave a comment and I will help you out to the best of my ability!如果您有任何问题,请随时发表评论,我会尽我所能帮助您!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM