javascript preg:如何编写 preg 模式以从电子邮件中获取域名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5181746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
preg: how to write a preg pattern to get domain name from an email?
提问by Vin
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.
从像[email protected]我想获取域名的电子邮件地址gmail.com。我想在 Javascript 中的文本框值上使用该模式。
Pease suggest me an optimized and faster preg pattern for above requirement...
Pease 建议我为上述要求优化和更快的 preg 模式...
回答by Andy
You can replace everything up to and including the @symbol to get the domain. In Javascript:
您可以替换包括@符号在内的所有内容以获取域。在 JavaScript 中:
var email = '[email protected]';
var domain = email.replace(/.*@/, "");
alert(domain);
回答by Zoidberg
Why not just do this.
为什么不这样做。
var email = "[email protected]", i = email.indexOf("@");
if (i != -1) {
email = email.substring(i);
}
Regex isn't really required, you could also go email = email.split("@")[1];
正则表达式不是真的需要,你也可以去 email = email.split("@")[1];
回答by Develoger
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@[email protected]";
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.
因此,如果电子邮件有多个 @ 字符,那么您只需将电子邮件字符串按“@”拆分并计算新创建的数组中有多少个元素,然后从中减去 1,您就可以从具有该数字的数组中取出正确的元素。
Here is the jsfiddle: http://jsfiddle.net/47yqn/
这是 jsfiddle:http: //jsfiddle.net/47yqn/
It has show 100% success for me!
它对我来说是 100% 的成功!
回答by Jesse Cohen
Using a simple string split won't work on addresses like 'abc@abc'@example.comwhich is a valid address (technically). I believe splitting on @and taking the lastelement should be fine, because no @characters are allowed to appear in the domain.
使用简单的字符串拆分不适'abc@abc'@example.com用于有效地址(技术上)这样的地址。我相信拆分@并取最后一个元素应该没问题,因为@域中不允许出现任何字符。
Or, since you requested, a regex:
[^@]+$
或者,根据您的要求,使用正则表达式:
[^@]+$
回答by Partha Roy
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.
这将从任何电子邮件中获取域。
回答by mruanova
var email = '[email protected]';
var domain = email.replace(/.*@/, "").split('.')[0];
console.log(domain); // gmail
回答by M'vy
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.
或者也许可以稍微调整一下,用and替换\bs 。有了这个,您可以匹配任何带有 AZ、az 和 0-9 字符的域。^$
回答by user8055598
You can do this to get domain name from url,email,website,with http started,only domain name
您可以这样做以从 url、电子邮件、网站、http 启动、仅域名获取域名
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;

