删除带有特定符号的句子中的所有单词

removing all words in a sentences with a specific symbol

本文关键字:句子 单词 符号 删除      更新时间:2023-09-26

我正试图删除一个以符号$#开头的句子中的所有单词,例如,如果有一个句子I am $#trying to $#learn $regex for $#javascript我想删除单词trying、learning和javascript。。有可能通过javascript正则表达式实现吗??

var sentence = "I am $#trying to $#learn $regex for $#javascript";
sentence = sentence.replace(/'$#[^'s]+/g, '');
// output: "I am  to  $regex for "

除了之外,还需要修剪字符串和重复的空格

sentence = sentence
  .replace(/'s*'$#[^'s]+'s*/g, ' ')
  .replace(/^'s+|'s+$/g, '')
;
// output: "I am to $regex for"