如何从URL提取顶级域名?

时间:2019-09-26 17:43:38

标签: javascript

我有一个不同的域,例如:

https://www.google.com
https://www.google.de
https://www.google.co.uk
https://www.google.com/randompath
https://www.google.de/randompath
https://www.google.co.uk/randompath

对于每个可能的链接,我只需要提取顶级域。对于此示例,它将是.com .de .co.uk ....

我尝试过的正则表达式:

/\.[^.]{2,3}(?:\.[^.]{2,3})?$/

仅当顶级域之后没有其他路径时才有效。有人有解决方案吗?

1 个答案:

答案 0 :(得分:0)

使用URL

此域名不会获得not trivial的二级域名,而无需查找表就可以正确访问

var domains =`https://www.google.com
https://www.google.de
https://www.google.co.uk
https://www.google.com/randompath
https://www.google.de/randompath
https://www.google.co.uk/randompath`
  .split(/\n/)
  .map(href => new URL(href).hostname.split(".").pop())

console.log(domains)

根据长度,这最后一个或最后两个

var domains = `https://www.google.com
https://www.google.de
https://www.google.co.uk
https://foo.bar.my.subdomain.example.co.uk 
https://www.google.com/randompath
https://www.google.de/randompath
https://www.google.co.uk/randompath`
  .split(/\n/)
  .map(href => {
    let hostnameParts = new URL(href).hostname.split(".");
    let domain = hostnameParts.slice(hostnameParts.length > 3 ? -2 : -1)
    return domain.join(".");
  })

console.log(domains)