最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Given a URL as a string, how to extract just the domain and extension? - Stack Overflow

programmeradmin3浏览0评论

Given a string with URLs in the following formats:

/
/
.html
/?hpt=ed_Intl

W JS/jQuery, how can I extract from the string just cnn for all of them? Top level domain plus extension?

Thanks

Given a string with URLs in the following formats:

https://www.cnn./
http://www.cnn./
http://www.cnn./2012/02/16/world/american-nicaragua-prison/index.html
http://edition.cnn./?hpt=ed_Intl

W JS/jQuery, how can I extract from the string just cnn. for all of them? Top level domain plus extension?

Thanks

Share Improve this question asked Feb 20, 2012 at 0:17 AnApprenticeAnApprentice 111k202 gold badges637 silver badges1k bronze badges 3
  • 2 url.match(/:\/\/(.[^/]+)/)[1]; – AnApprentice Commented Feb 20, 2012 at 0:23
  • Why bother with regex. It's a Swiss army knife when all you need is a spoon. – tkone Commented Feb 20, 2012 at 0:26
  • @AnApprentice if you want subdomains, just use location.host. No need for regex at alla. – tkone Commented Feb 20, 2012 at 0:31
Add a ment  | 

5 Answers 5

Reset to default 3
​var loc = document.createElement('a');

loc.href = 'http://www.cnn./2012/02/16/world/index.html';

​window.alert(loc.hostname);​ // alerts "cnn."

Credits for the previous method:

Creating a new Location object in javascript

function domain(input){
    var matches,
        output = "",
        urls = /\w+:\/\/([\w|\.]+)/;

    matches = urls.exec(input);

    if(matches !== null){
        output = matches[1];
    }

    return output;
}

Given that there are top-level domains with dots in them, for example "co.uk", there's no way to do this programatically unless you include a list of all of the TLDs with dots in them.

var domain = location.host.split('.').slice(-2);

If you want it reassembled:

var domain = location.host.split('.').slice(-2).join('.');

But this won't work with co.uk or something. There's no hard nor fast rule for this, not even regex will determine that.

// something.domain. -> domain.
function getDomain() {
  return window.location.hostname.replace(/([a-z]+.)/,"");
}
发布评论

评论列表(0)

  1. 暂无评论