mixa_ru
mixa_ru

Reputation: 603

Javascript. How to extract URI encoded email from string?

var string = 'https://opt.portal.co.uk/index/stepregistration/Username/test%40test.es/nextstep/1/lang/es?kmi=K54Nv1RdlV71hhiLEdPg0UZ0%3D&_ga=1.217245974.18890806.1485212';

Email is encoded like so test%40test.es

What is the way to get this email address with JavaScript (no jQuery)?

Upvotes: 0

Views: 2657

Answers (2)

Nathan Hase
Nathan Hase

Reputation: 659

decodeURIComponent(string)

example from w3c:

var uri = "https://w3schools.com/my test.asp?name=ståle&car=saab";
var uri_enc = encodeURIComponent(uri);
var uri_dec = decodeURIComponent(uri_enc);
var res = uri_enc + "<br>" + uri_dec;

https://www.w3schools.com/jsref/jsref_decodeuricomponent.asp

In your example you'll want to extract the username from the url. Querystring or Hash key/value pairs might be easier to deal with but you can use a regular expression or split the url by '/' and loop the result to find the element after the 'Username' element.

Upvotes: 1

m87
m87

Reputation: 4523

You could accomplish this using the following way :

var string = 'https://opt.portal.co.uk/index/stepregistration/Username/test%40test.es/nextstep/1/lang/es?kmi=K54Nv1RdlV71hhiLEdPg0UZ0%3D&_ga=1.217245974.18890806.1485212';
var email = decodeURIComponent(string).match(/\w+@\w+\.\w+/g)[0];
console.log(email);

Upvotes: 4

Related Questions