24

From an email address like something@gmail.com 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...

Vinay Jeurkar
  • 3,054
  • 9
  • 37
  • 56
  • 2
    sorry, do you want a preg regex or a JS regex, they are different and potentially incompatible. also, what have you tried? – tobyodavies Mar 03 '11 at 14:07
  • I was told that regular expression works faster than string functions. I dont know much about regular expression. :( – Vinay Jeurkar Mar 03 '11 at 14:12

9 Answers9

64

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

var email = 'test@gmail.com';
var domain = email.replace(/.*@/, "");
alert(domain);
Andy
  • 17,423
  • 9
  • 52
  • 69
  • That won't work, see Jesse Cohen's comment below. There can be multiple @ characters in an email address (albeit between quotation marks) – Willem Mulder Oct 01 '13 at 19:32
  • thanks for the regex but it will only work for - something.com ( without subdomains) . I have posted a solution extending yours which supports domain fetching from any kind of url ( n number of subdomain level) . – Partha Roy Apr 04 '17 at 16:50
7

Why not just do this.

var email = "something@gmail.com", i = email.indexOf("@");
if (i != -1) {
   email = email.substring(i);
}

Regex isn't really required, you could also go email = email.split("@")[1];

Zoidberg
  • 10,137
  • 2
  • 31
  • 53
  • 5
    To address problem raised by Jesse Cohen, you can use `email.lastIndexOf('@')+1` to get the last `@` position. See https://jsfiddle.net/mx6bqytg/1/ – Maxime Pacary Apr 14 '17 at 08:23
5

I have just experience a need to implement this and came up with the solution that combines most of already mentioned techniques:

var email = "test@test@gmail.com";
var email_string_array = email.split("@");
var domain_string_location = email_string_array.length -1;
var final_domain = email_string_array[domain_string_location];

So if email has multiple @ characters then you just need to split email string by "@" and calculate how many elements are there in new created array then subtract 1 from it and you can take right element from array with that number.

Here is the jsfiddle: http://jsfiddle.net/47yqn/

It has show 100% success for me!

Develoger
  • 3,950
  • 2
  • 24
  • 38
4

Using a simple string split won't work on addresses like 'abc@abc'@example.com which is a valid address (technically). I believe splitting on @ and taking the last element should be fine, because no @ characters are allowed to appear in the domain.

Or, since you requested, a regex: [^@]+$

Jesse Cohen
  • 4,010
  • 22
  • 25
3

A bit cleaner and up-to-date approach:

const email = "something@gmail.com"
const domain = email.includes('@') && email.split("@").pop()

Domain will be false if email doesn't contain @ symbol.

BorisS
  • 3,148
  • 1
  • 14
  • 12
2

You can do this

var extract_company_name = function(email){
  var temp = email.replace(/.*@/, '').split('.');
    return temp[temp.length - 2];
}
extract_company_name(email)

this will fetch the domain from any email.

code in jsbin

Partha Roy
  • 1,575
  • 15
  • 16
1
var email = 'test@gmail.com';
var domain = email.replace(/.*@/, "").split('.')[0];
console.log(domain); // gmail
mruanova
  • 6,351
  • 6
  • 37
  • 55
1

I would try

\b.*@([A-Za-z0-9.-]+\.[A-Za-z]{2,4})\b

Or maybe tune it a little replacing \bs by ^ and $. With this you can match any domain with A-Z, a-z and 0-9 characters.

M'vy
  • 5,696
  • 2
  • 30
  • 43
-1

You can do this to get domain name from url,email,website,with http started,only domain name

var str=inputAddress;
      var patt1 = "(http://|https://|ftp://|www.|[a-z0-9._%+-]+@)([^/\r\n]+)(/[^\r\n]*)?";
var result = str.match(patt1);
var domain=result===null?str:result[2];
return domain.toString().startsWith("www.")?domain.toString().slice(4):domain;
litelite
  • 2,857
  • 4
  • 23
  • 33
  • 1
    I don't think it's a good idea to make a regex do so much. They easily become prone to errors. In this case, an e-mail like `hello@www.example.com` would give a domain `example.com`, but the correct domain is `www.example.com`. – Vlad V May 23 '17 at 20:55