如果字符串中包含单词,请删除它们

时间:2019-06-27 12:24:46

标签: javascript

我有一个字符串和一个数组中的单词列表。

我要说的是“如果此数组中的任何单词在字符串中,请将其删除”,然后“从字符串中删除双倍空格”

我快到了,但是由于某种原因,它没有注意破折号

SELECT * FROM tables
WHERE convert(varchar(10), time, 102) = convert(varchar(10), getdate(), 102)

这将返回const name = "My Awesome T-Shirt Something Else" ; const words = [ 'V-Neck ', 'Long Sleeve ', 'T-Shirt ', 'Pullover Hoodie ', 'Raglan Baseball Tee ', 'Tee ', 'Zip Hoodie ', 'Tank Top ', 'Premium ', 'Sweatshirt ', 'PopSockets Grip and Stand for Phones and Tablets ', 'Shirt ' ]; let newName = name; words.forEach(w => { if(name.includes(w)) newName = name.replace(w, ''); }); newName = newName.replace(/ +(?= )/g,''); console.log(newName)

1 个答案:

答案 0 :(得分:5)

您要替换name而不是newName

const name = "My Awesome T-Shirt Something Else"    ;
const words = [
      'V-Neck ',
      'Long Sleeve ',
      'T-Shirt ',
      'Pullover Hoodie ',
      'Raglan Baseball Tee ',
      'Tee ',
      'Zip Hoodie ',
      'Tank Top ',
      'Premium ',
      'Sweatshirt ',
      'PopSockets Grip and Stand for Phones and Tablets ',
      'Shirt '
    ];

let newName = name;


words.forEach(w => {
   while (newName.includes(w)) newName = newName.replace(w, ''); // take a while for more than one occurences
   //     ^^^^^^^                        ^^^^^^^
});

newName = newName.replace(/ +(?= )/g,'');

console.log(newName)