Regex URL Path from URL

后端 未结 9 2820
情歌与酒
情歌与酒 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:29

    var subject =
    '<link rel="shortcut icon" href="https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=ec617d715196"><link rel="apple-touch-icon" href="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a"><link rel="image_src" href="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a">';
    var re=/\"[a-z]+:\/\/[^ ]+"/m;
    document.write(subject.match(re));

    You can try this

    /\"[a-z]+:\/\/[^ ]+/
    

    Usage

    if (/\"[a-z]+:\/\/[^ ]+/m.test(subject)) {  // Successful match } else {    // Match attempt failed }
    
    0 讨论(0)
  • 2021-02-19 14:32

    In case if you need this for your JavaScript web-app: the best answer I ever found on this topic is here. Basic (and also original) version of the code looks like this:

    var parser = document.createElement('a');
    parser.href = "http://example.com:3000/pathname/?search=test#hash";
    
    parser.protocol; // => "http:"
    parser.hostname; // => "example.com"
    parser.port;     // => "3000"
    parser.pathname; // => "/pathname/"
    parser.search;   // => "?search=test"
    parser.hash;     // => "#hash"
    parser.host;     // => "example.com:3000"
    

    Thank you John Long, you made by day!

    0 讨论(0)
  • 2021-02-19 14:37

    Its not a regex solution, but most languages have a URL library that will parse any URL into its constituent parts. This may be a better solution for what you are doing.

    0 讨论(0)
提交回复
热议问题