0

我正在尝试删除以符号开头的句子中的所有单词$#,例如,如果有一个句子I am $#trying to $#learn $regex for $#javascript 我想删除单词 try , learn 和 javascript .. 是否可以通过 javascript regex 实现?

4

2 回答 2

1
var sentence = "I am $#trying to $#learn $regex for $#javascript";
sentence = sentence.replace(/\$#[^\s]+/g, '');
// output: "I am  to  $regex for "
于 2013-07-16T08:21:46.233 回答
0

此外,还需要修剪字符串和重复的空格:

sentence = sentence
  .replace(/\s*\$#[^\s]+\s*/g, ' ')
  .replace(/^\s+|\s+$/g, '')
;
// output: "I am to $regex for"
于 2013-07-16T08:45:15.007 回答