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

时间:2013-07-16 08:19:17

标签: javascript jquery regex

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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