KingJulian
KingJulian

Reputation: 973

RegEx match only final domain name from any email address

I want to match only parent domain name from an email address, which might or might not have a subdomain.

So far I have tried this:

new RegExp(/.+@(:?.+\..+)/);

The results:

Input: [email protected]
Output: ["[email protected]", "subdomain.maindomain.com"]

Input: [email protected]
Output: ["[email protected]", "maindomain.com"]

I am interested in the second match (the group).
My objective is that in both cases, I want the group to match and give me only maindomain.com

Note: before the down vote, please note that neither have I been able to use existing answers, nor the question matches existing ones.

Upvotes: 2

Views: 735

Answers (3)

charsi
charsi

Reputation: 3877

With the following maindomain should always return the maindomain.com bit of the string.

var pattern = new RegExp(/(?:[\.@])(\w[\w-]*\w\.\w*)$/);

var str = "[email protected]";
var maindomain = str.match(pattern)[1];

http://codepen.io/anon/pen/RRvWkr

EDIT: tweaked to disallow domains starting with a hyphen i.e - '-yahoo.com'

Upvotes: 0

anubhava
anubhava

Reputation: 785481

You can use this regex:

/@(?:[^.\s]+\.)*([^.\s]+\.[^.\s]+)$/gm

Use captured group #1 for your result.

  • It matches @ followed by 0 or more instance of non-DOT text and a DOT i.e. (?:[^.\s]+\.)*.
  • Using ([^.\s]+\.[^.\s]+)$ it is matching and capturing last 2 components separated by a DOT.

RegEx Demo

Upvotes: 1

hugomg
hugomg

Reputation: 69954

One simple regex you can use to get only the last 2 parts of the domain name is

/[^.]+\.[^.]$/

It matches a sequence of non-period characters, followed by period and another sequence of non-periods, all at the end of the string. This regex doesn't ensure that this domain name happens after a "@". If you want to make a regex that also does that, you could use lazy matching with "*?":

/@.*?([^.]+\.[^.])$/

However,I think that trying to do everything at once tends to make the make regexes more complicated and hard to read. In this problem I would prefer to do things in two steps: First check that the email has an "@" in it. Then you get the part after the "@" and pass it to the simple regex, which will extract the domain name.

One advantage of separating things is that some changes are easier. For example, if you want to make sure that your email only has a single "@" in it its very easy to do in a separate step but would be tricky to achieve in the "do everything" regex.

Upvotes: 2

Related Questions