开发者

How can you get subdomain.site.com and www.site.com down to the domain name

In javascript is it possible to create a function that checks an url to find the domain name. EG: if these sites:

www.site.co.uk

www.subdomain.site.co.uk

site.co.uk

s开发者_StackOverflow中文版ubdomain.site.co.uk

were put into the function they would be returned as "site.co.uk".


I'd say there is a regex solution to this, but it will be complicated. You will have to find the last occurrence of the '.' and get what's after that, then you should get what's between the last and second to last '.', then get anything before the second to last period, up to the third to the right (subdomain) '.'.

Does that make sense?? I'm not sure of the regex required, but here are some questions on SO to get you started...

Regex: To pull out a sub-string between two tags in a string

Regex to replace all \n in a String, but no those inside [code] [/code] tag

Need regexp to find substring between two tokens

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜