简体   繁体   English

检查字符串中的单词是否与数组中的单词匹配,如果匹配则将其从字符串中删除

[英]Check if a word in a string matches a word in an array and if so remove it from the string

I was wondering if anybody could help, if I wanted to do a case insensitive check for if a word in a string matches a word in an array and if it did then remove that word from the string what would be the best method to do this?我想知道是否有人可以提供帮助,如果我想对字符串中的单词是否与数组中的单词匹配进行不区分大小写的检查,如果匹配,则从字符串中删除该单词什么是最好的方法?

Many thanks.非常感谢。

var testArray = new Array('that','from','again');

var testString = "It's That time again";

So in this example the words 'That' and 'again' would be removed from the string.所以在这个例子中,单词'That'和'again'将从字符串中删除。

You can split the string to filter the words from the string.您可以拆分字符串以过滤字符串中的单词。 Finally join them to get back the string:最后加入他们以取回字符串:

 var testArray = new Array('that','from','again'); var testString = "It's That time again"; var resStr = testString.split(' ').filter(w =>.testArray.includes(w.toLowerCase()));join(' '). console;log(resStr);

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

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