preg: how to write a preg pattern to get domain name from an email?

Vin picture Vin · Mar 3, 2011 · Viewed 19.8k times · Source

From an email address like [email protected] I want to fetch domain name gmail.com. i want to use that pattern on textbox value in Javascript.

Pease suggest me an optimized and faster preg pattern for above requirement...

Answer

Andy picture Andy · Mar 3, 2011

You can replace everything up to and including the @ symbol to get the domain. In Javascript:

var email = '[email protected]';
var domain = email.replace(/.*@/, "");
alert(domain);