Search code examples
javascriptregexpreg-match

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


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...


Solution

  • 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);