-3

my input is var email = "xyz+wex+rr%40gmail.com";

i need output as

xyz wex rr @ gmail.com

i have tried with this below regex , i can only remove + from my string how to replace %40 with @

email .replace(/+/g, " ");

3
  • 1
    You will have to escape characters that have special meaning in regex like +. Try /\+/g Commented Jun 9, 2017 at 7:49
  • Simply run another replace with "\%40" as search string to be replaced. Commented Jun 9, 2017 at 7:51
  • Look at this stackoverflow.com/a/20247435/2329980 Commented Jun 9, 2017 at 7:53

3 Answers 3

3

var email = "xyz+wex+rr%40gmail.com";
email = decodeURIComponent(email).replace(/\+/g, " ");
console.log(email);

decodeURIComponent decodes things like %40. It only does not replace the + signs with spaces, so this is done with a RegEx, escaping the plus sign, which has a special meaning in RegEx.

Sign up to request clarification or add additional context in comments.

Comments

1

Here is required solution,

email.replace(/\+/g, " ").replace(/\%40/g, "@")

var email = "xyz+wex+rr%40gmail.com";
console.log(email.replace(/\+/g, " ").replace(/\%40/g, "@"))

Result: xyz wex [email protected]

Please run the above snippet

Comments

1

You can use unescape function to first unescape the string and than replace +with

Try this one

var email = unescape("xyz+wex+rr%40gmail.com").replace(/[+]/g, " ");
console.log(email)

As @Florian Albrecht said that unescape is deprecated so deprecated Florian's answer is better

2 Comments

thanks for sharing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.