Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve Name from Email Address

Tags:

javascript

With javascript, how do we remove the @gmail.com or @aol.com from a string so that what only remains is the name?

var string = "[email protected]"; 

Will be just "johdoe"? I tried with split but it did not end well. thanks.

like image 777
leeshin Avatar asked Aug 22 '13 03:08

leeshin


People also ask

How do I find my first name and last name in an email?

FIND(“.”,Email) will find the location of the first period in the email going from the left to right direction in the email address. This is the location of the period that separates the first name from the last name.


2 Answers

var email = "[email protected]"; var name   = email.substring(0, email.lastIndexOf("@")); var domain = email.substring(email.lastIndexOf("@") +1);  console.log( name );   // john.doe console.log( domain ); // example.com 

The above will also work for valid names containing @ (tools.ietf.org/html/rfc3696Page 5):

john@doe
"john@@".doe
"j@hn".d@e

  • String.prototype.substring()
  • String.prototype.lastIndexOf()

Using RegExp:

Given the email value is already validated, String.prototype.match() can be than used to retrieve the desired name, domain:

String match:

const name   = email.match(/^.+(?=@)/)[0];     const domain = email.match(/(?<=.+@)[^@]+$/)[0];  

Capturing Group:

const name   = email.match(/(.+)@/)[1];     const domain = email.match(/.+@(.+)/)[1]; 

To get both fragments in an Array, use String.prototype.split() to split the string at the last @ character:

const [name, domain] = email.split(/(?<=^.+)@(?=[^@]+$)/); console.log(name, domain); 

or simply with /@(?=[^@]*$)/.
Here's an example that uses a reusable function getEmailFragments( String )

const getEmailFragments = (email) => email.split(/@(?=[^@]*$)/);  [ // LIST OF VALID EMAILS:   `[email protected]`,   `john@[email protected]`,   `"john@@"[email protected]`,   `"j@hn".d@[email protected]`, ] .forEach(email => {   const [name, domain] = getEmailFragments(email);   console.log("DOMAIN: %s NAME: %s ", domain, name); });
like image 88
Roko C. Buljan Avatar answered Oct 09 '22 09:10

Roko C. Buljan


You should take note that a valid email address is an incredibly sophisticated object and may contain multiple @ signs (ref. http://cr.yp.to/im/address.html).

"The domain part of an address is everything after the final @."

Thus, you should do something equivalent to:

var email = "[email protected]"; var name = email.substring(0, email.lastIndexOf("@")); 

or even shorter,

var name = email.replace(/@[^@]+$/, ''); 

If you want both the name and the domain/hostname, then this will work:

var email = "[email protected]"; var lasta = email.lastIndexOf('@'); var name, host; if (lasta != -1) {     name = email.substring(0, lasta);     host = email.substring(lasta+1);     /* automatically extends to end of string when 2nd arg omitted */ } else {     /* respond to invalid email in some way */ } 
like image 32
Joseph Myers Avatar answered Oct 09 '22 11:10

Joseph Myers