简体   繁体   English

RegEx仅匹配来自任何电子邮件地址的最终域名

[英]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 我的目标是,在两种情况下,我都希望组匹配并只给我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 您可以使用一个简单的正则表达式仅获取域名的后2个部分:

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

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. 但是,我认为尝试立即执行所有操作往往会使make正则表达式更加复杂且难以阅读。 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. 将捕获的组#1用于结果。

  • It matches @ followed by 0 or more instance of non-DOT text and a DOT ie (?:[^.\\s]+\\.)* . 它匹配@后跟0或多个非DOT文本实例和一个DOT,即(?:[^.\\s]+\\.)*
  • Using ([^.\\s]+\\.[^.\\s]+)$ it is matching and capturing last 2 components separated by a DOT . 使用([^.\\s]+\\.[^.\\s]+)$可以匹配并捕获由DOT分隔的最后两个分量。

RegEx Demo 正则演示

With the following maindomain should always return the maindomain.com bit of the string. 使用以下maindomain应始终返回字符串的maindomain.com位。

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 http://codepen.io/anon/pen/RRvWkr

EDIT: tweaked to disallow domains starting with a hyphen ie - '-yahoo.com' 编辑:进行了调整,以禁止以连字符开头的域,即-'-yahoo.com'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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