Extract hostname name from string

后端 未结 28 1509
情歌与酒
情歌与酒 2020-11-22 07:15

I would like to match just the root of a URL and not the whole URL from a text string. Given:

http://www.youtube.co         


        
28条回答
  •  悲&欢浪女
    2020-11-22 08:17

    in short way you can do like this

    var url = "http://www.someurl.com/support/feature"
    
    function getDomain(url){
      domain=url.split("//")[1];
      return domain.split("/")[0];
    }
    eg:
      getDomain("http://www.example.com/page/1")
    
      output:
       "www.example.com"
    

    Use above function to get domain name

提交回复
热议问题