Get second level domain name from URL

前端 未结 1 1273
眼角桃花
眼角桃花 2021-02-15 03:52

Is there a way to get top level domain name from the url

for e.g., \"https://images.google.com/blah\" => \"google\"

I found this:

var domain = n         


        
1条回答
  •  太阳男子
    2021-02-15 04:16

    You could do this:

    location.hostname.split('.').pop()
    

    EDIT

    Saw the change to your question, you would need a list of all TLDs to match against and remove from the hostname, then you could use split('.').pop()

    // small example list
    var re = new RegExp('\.+(co.uk|me|com|us)')
    var secondLevelDomain = 'https://www.google.co.uk'.replace(re, '').split('.').pop()
    

    0 讨论(0)
提交回复
热议问题