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

Aravind E
  • 1,031
  • 3
  • 15
  • 25

3 Answers3

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.

Florian Albrecht
  • 2,266
  • 1
  • 20
  • 25
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

Nauman Umer
  • 1,006
  • 1
  • 11
  • 21
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 rr@gmail.com

Please run the above snippet

Sravan
  • 18,467
  • 3
  • 30
  • 54