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.
['https://Stackoverflow.com/questions/18371339', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2522342/']
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 */
}

```
This simple regex will do the needful.  

`/^.*(?=@)/g`.

Example:

```
"johndoe@yahoo.com".match(/^.*(?=@)/g); // returns Array [ "johndoe" ]

```