使用javascript替换@后面的文本

时间:2015-04-01 18:10:11

标签: javascript regex

我想创建一个JavaScript函数来替换后跟@

的单词

示例:

              var sample = "Hello @Jon how are you";
              var result  = myfunction(sample);
              // result should be like "Hello xxxxxx how are you"

此处@符号也需要更换。

2 个答案:

答案 0 :(得分:2)

@\S+

试试这个。xxx。见。演示。

https://regex101.com/r/sJ9gM7/38

var re = /@\S+/gm;
var str = 'Hello @Jon how are you';
var subst = 'x';

var result = str.replace(re, subst);

答案 1 :(得分:2)

你可以试试这个正则表达式,

@(\w+)

Working Demo

相关问题