Sonnu
Sonnu

Reputation: 35

Replace text followed by @ using javascript

I want to create a JavaScript function which replace the word followed by @

Example:

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

Here @ symbol also need to be replaced.

Upvotes: 1

Views: 33

Answers (2)

apgp88
apgp88

Reputation: 985

You can try this regex,

@(\w+)

Working Demo

Upvotes: 2

vks
vks

Reputation: 67968

@\S+

Try this.Replace by xxx.See demo.

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);

Upvotes: 2

Related Questions