如果字符串中包含单词,请删除它们。
给我们一个字符串和一个字符串数组;我们的工作是编写一个函数,从字符串中删除数组中存在的所有子字符串。
这些子字符串是完整的单词,因此我们也应该删除开头或结尾的空格,以免两个空格一起出现。
因此,让我们为该函数编写代码-
示例
const string = "The weather in Delhi today is very similar to the weather
in Mumbai";
const words = [
'shimla','rain','weather','Mumbai','Pune','Delhi','tomorrow','today','yesterday'
];
const removeWords = (str, arr) => {
return arr.reduce((acc, val) => {
const regex = new RegExp(` ${val}`, "g");
return acc.replace(regex, '');
}, str);
};
console.log(removeWords(string, words));输出结果
此代码的输出将是-
The in is very similar to the in
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短