Given the following email address -- [email protected] -- how can I extract someone from the address using javascript?
Thank you.
Web browsers display interactive, dynamic content, and they update often. But interactive elements like Flash, JavaScript, or HTML forms won't work in most email inboxes.
Regular Expression with match
with safety checks
var str="[email protected]"; var nameMatch = str.match(/^([^@]*)@/); var name = nameMatch ? nameMatch[1] : null;
written as one line
var name = str.match(/^([^@]*)@/)[1];
Regular Expression with replace
with safety checks
var str="[email protected]"; var nameReplace = str.replace(/@.*$/,""); var name = nameReplace!==str ? nameReplace : null;
written as one line
var name = str.replace(/@.*$/,"");
Split String
with safety checks
var str="[email protected]"; var nameParts = str.split("@"); var name = nameParts.length==2 ? nameParts[0] : null;
written as one line
var name = str.split("@")[0];
Performance Tests of each example
JSPerf Tests
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With