Reputation: 536
I've tested my regex on Regex testers and it worked, but I didn't get it to work on my code.
var mail = "[email protected]";
var regExp = new RegExp("@(.*?)\.");
document.write(regExp.exec(mail)) ;
I get this result :
@g,
I tried to add a backslash before the dot, and I got this :
@gmail.,gmail
I also wanted to remove the "@" and the "." from the email, so I tried to use " (?:@) ", but I didn't get it to work (on Regex testers).
It's my first time trying to use Regex, and I don't get it. Why is there a comma ?
Upvotes: 1
Views: 122
Reputation: 707328
A couple things to do differently:
/regex here/
syntax.Here's the code:
var mail = "[email protected]";
console.log(mail.match(/@(.*?)\./)[1]);
Upvotes: 1
Reputation: 1373
Faster than regex:
var emailAddress = "[email protected]";
var array_email = emailAddress.split("@");
alert('Account: ' + array_email[0] +'; Domain: ' + array_email[1]);
Upvotes: 1