简体   繁体   中英

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: abc@subdomain.maindomain.com
Output: ["abc@subdomain.domain.com", "subdomain.maindomain.com"]

Input: abc@maindomain.com
Output: ["abc@maindomain.com", "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.

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.

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 ie (?:[^.\\s]+\\.)* .
  • Using ([^.\\s]+\\.[^.\\s]+)$ it is matching and capturing last 2 components separated by a DOT .

RegEx Demo

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

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

var str = "abc@subdomain.maindomain.com";
var maindomain = str.match(pattern)[1];

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM