How to get the domain name from anchor href attribute?

后端 未结 4 1924
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-22 00:56

I have a url like e.g.:

http://www.intereconomia.com/noticias-gaceta/politica/grinan-los-casos-corrupcion-pueden-influir-20120226

I want on

4条回答
  •  伪装坚强ぢ
    2020-12-22 01:39

    Instead of using a regex you can try this which is clean and simple.

    $('a').each(function(){
        $(this).text(this.protocol + "//" + (this.hostname || this.pathname));
    });​
    

    Note: If you want to set it only for set of anchors, then change the selector accordingly but the logic inside remains the same.

    Working demo - http://jsfiddle.net/ShankarSangoli/8G7JM/3/

提交回复
热议问题