Javascript/Regex 用于仅查找没有子域的根域名 [英] Javascript/Regex for finding just the root domain name without sub domains
问题描述
我进行了搜索,发现了很多类似的正则表达式示例,但不是我所需要的.
我希望能够传入以下网址并返回结果:
www.google.com 返回 google.com
sub.domains.are.cool.google.com 返回 google.com
doesntmatterhowlongasubdomainis.idont.wantit.google.com返回 google.com
sub.domain.google.com/no/thanks 返回 google.com
希望这是有道理的:)提前致谢!-詹姆斯
你不能用正则表达式来做到这一点,因为你不知道后缀中有多少个块.
例如,google.com 的后缀是 com.要从 subdomain.google.com 到 google.com,您必须使用最后两个块 - 一个用于后缀,一个用于 google强>.
如果您将此逻辑应用于 subdomain.google.co.uk,尽管您最终会得到 co.uk.
您实际上需要从http://publicsuffix.org/之类的列表中查找后缀>
I had a search and found lot's of similar regex examples, but not quite what I need.
I want to be able to pass in the following urls and return the results:
www.google.com returns google.com
sub.domains.are.cool.google.com returns google.com
doesntmatterhowlongasubdomainis.idont.wantit.google.com returns google.com
sub.domain.google.com/no/thanks returns google.com
Hope that makes sense :) Thanks in advance!-James
You can't do this with a regular expression because you don't know how many blocks are in the suffix.
For example google.com has a suffix of com. To get from subdomain.google.com to google.com you'd have to take the last two blocks - one for the suffix and one for google.
If you apply this logic to subdomain.google.co.uk though you would end up with co.uk.
You will actually need to look up the suffix from a list like http://publicsuffix.org/
这篇关于Javascript/Regex 用于仅查找没有子域的根域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!