Regex URL Path from URL

后端 未结 9 2865
情歌与酒
情歌与酒 2021-02-19 13:57

I am having a little bit of regex trouble.

I am trying to get the path in this url videoplay.

http://video.google.co.uk:80/videoplay?docid=-         


        
9条回答
  •  感情败类
    2021-02-19 14:26

    function getPath(url, defaults){
        var reUrlPath = /(?:\w+:)?\/\/[^\/]+([^?#]+)/;
        var urlParts = url.match(reUrlPath) || [url, defaults];
        return urlParts.pop();
    }
    alert( getPath('http://stackoverflow.com/q/123/regex-url', 'unknown') );
    alert( getPath('https://stackoverflow.com/q/123/regex-url', 'unknown') );
    alert( getPath('//stackoverflow.com/q/123/regex-url', 'unknown') );
    alert( getPath('http://stackoverflow.com/q/123/regex-url?foo', 'unknown') );
    alert( getPath('http://stackoverflow.com/q/123/regex-url#foo', 'unknown') );
    alert( getPath('http://stackoverflow.com/q/123/regex-url/', 'unknown') );
    alert( getPath('http://stackoverflow.com/q/123/regex-url/?foo', 'unknown') );
    alert( getPath('http://stackoverflow.com/q/123/regex-url/#foo', 'unknown') );
    alert( getPath('http://stackoverflow.com/', 'unknown') );

提交回复
热议问题