Extract hostname name from string

后端 未结 28 1464
情歌与酒
情歌与酒 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 07:54

    Well, doing using an regular expression will be a lot easier:

        mainUrl = "http://www.mywebsite.com/mypath/to/folder";
        urlParts = /^(?:\w+\:\/\/)?([^\/]+)(.*)$/.exec(mainUrl);
        host = Fragment[1]; // www.mywebsite.com
    

提交回复
热议问题