1

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.

4
  • how many xxxx should be der Commented Apr 1, 2015 at 18:10
  • result = sample.replace(/@\w+/, 'xxxxx'); Commented Apr 1, 2015 at 18:14
  • @vks that is our wish Commented Apr 1, 2015 at 18:15
  • @anubhava yes xxxx will vary Commented Apr 1, 2015 at 18:27

2 Answers 2

2
@\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);
Sign up to request clarification or add additional context in comments.

Comments

2

You can try this regex,

@(\w+)

Working Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.