简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM