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 = "johndoe@yahoo.com";
Will be just "johdoe"? I tried with split but it did not end well. thanks.
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 = "johndoe@yahoo.com";
Will be just "johdoe"? I tried with split but it did not end well. thanks.
var email = "john.doe@example.com";
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
Given the email value is already validated, String.prototype.match() can be than used to retrieve the desired name, domain:
const name = email.match(/^.+(?=@)/)[0];
const domain = email.match(/(?<=.+@)[^@]+$/)[0];
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:
`info@example.com`,
`john@doe@example.com`,
`"john@@".doe@example.com`,
`"j@hn".d@e@example.com`,
]
.forEach(email => {
const [name, domain] = getEmailFragments(email);
console.log("DOMAIN: %s NAME: %s ", domain, name);
});
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 = "johndoe@yahoo.com";
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 = "johndoe@yahoo.com";
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 */
}
And another alternative using split:
var email = "john.doe@email.com";
var sp = email.split('@');
console.log(sp[0]); // john.doe
console.log(sp[1]); // email.com
Try it using substring()
and indexOf()
var name = email.substring(0, email.indexOf("@"));
var email = "johndoe@yahoo.com";
email=email.replace(/@.*/,""); //returns string (the characters before @)
You can try with the replace() and regular expression. You can read more about replace() using regex here
var myEmail = 'johndoe@yahoo.com';
var name= myEmail.replace(/@.*/, "");
console.log(name);
This returns the string before @
As shown in How to remove or get domain name from an e-mail address in Javascript?, you can do it using the following code:
const getDomainFromEmail = email => {
let emailDomain = null;
const pos = email.search('@'); // get position of domain
if (pos > 0) {
emailDomain = email.slice(pos+1); // use the slice method to get domain name, "+1" mean domain does not include "@"
}
return emailDomain;
};
const yourEmail = "jonth.nt49@4codev.com"
console.log(getDomainFromEmail(yourEmail));
// result : 4codev.com
This simple regex will do the needful.
/^.*(?=@)/g
.
Example:
"johndoe@yahoo.com".match(/^.*(?=@)/g); // returns Array [ "johndoe" ]