gpt4 book ai didi

javascript - 如何从 Javascript 中的电子邮件地址获取域?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:10:20 24 4
gpt4 key购买 nike

我想用 Javascript 从电子邮件地址获取域部分。从电子邮件中提取域很容易,例如通过 split:“joe@example.com”,即 example.com

但是,电子邮件也有类似“joe@subdomain1.example.com.uk”的形式,其中的域是example.com.uk,而不是subdomain1.example。 com.uk.这里的问题是 subdomain1 可能被错误地认为是域的一部分。

我如何可靠地做到这一点?

最佳答案

乍一看,这确实不是一个微不足道的问题。幸运的是,有解决这个问题的库,tld-extract是一个流行的选择,它使用 Mozilla 的公共(public)后缀列表(一个基于志愿者的列表)。用法是

var parser = require('tld-extract');

console.log( parser("www.google.com") );
console.log( parser("google.co.uk") );
/**
* >> { tld: 'com', domain: 'google.com', sub: 'www' }
* >> { tld: 'co.uk', domain: 'google.co.uk', sub: '' }
*/

要从首先由 @ 字符分割的电子邮件地址中提取服务器地址部分,如下所示

const email = "john@sub.domain.com"
const address = email.split('@').pop()
const domain = parser(address).domain

有关问题解决方案的更多深入讨论请查看 README一个类似的 python 库。

tldextract on the other hand knows what all gTLDs and ccTLDs look like by looking up the currently living ones according to the Public Suffix List (PSL). So, given a URL, it knows its subdomain from its domain, and its domain from its country code.

确保了解公共(public)后缀列表上的列表 website并了解它是基于志愿者工作的,可能并不总是面面俱到。

The Public Suffix List is a cross-vendor initiative to provide an accurate list of domain name suffixes, maintained by the hard work of Mozilla volunteers and by submissions from registries, to whom we are very grateful.

Since there was and remains no algorithmic method of finding the highest level at which a domain may be registered for a particular top-level domain (the policies differ with each registry), the only method is to create a list. This is the aim of the Public Suffix List.

关于javascript - 如何从 Javascript 中的电子邮件地址获取域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49892932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com