Extract hostname name from string

后端 未结 28 1832
情歌与酒
情歌与酒 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:01

    Code:

    var regex = /\w+.(com|co\.kr|be)/ig;
    var urls = ['http://www.youtube.com/watch?v=ClkQA2Lb_iE',
                'http://youtu.be/ClkQA2Lb_iE',
                'http://www.example.com/12xy45',
                'http://example.com/random'];
    
    
    $.each(urls, function(index, url) {
        var convertedUrl = url.match(regex);
        console.log(convertedUrl);
    });
    

    Result:

    youtube.com
    youtu.be
    example.com
    example.com
    

提交回复
热议问题