Regex URL Path from URL

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

    This expression gets everything after videoplay, aka the url path.

    /\/(videoplay.+)/
    

    This expression gets everything after the port. Also consisting of the path.

    /\:\d./(.+)/
    

    However If using Node.js I recommend the native url module.

    var url = require('url')
    var youtubeUrl = "http://video.google.co.uk:80/videoplay?docid=-7246927612831078230&hl=en#hello"
    url.parse(youtubeUrl)
    

    Which does all of the regex work for you.

    {
      protocol: 'http:',
      slashes: true,
      auth: null,
      host: 'video.google.co.uk:80',
      port: '80',
      hostname: 'video.google.co.uk',
      hash: '#hello',
      search: '?docid=-7246927612831078230&hl=en',
      query: 'docid=-7246927612831078230&hl=en',
      pathname: '/videoplay',
      path: '/videoplay?docid=-7246927612831078230&hl=en',
      href: 'http://video.google.co.uk:80/videoplay?docid=-7246927612831078230&hl=en#hello' 
    }
    

提交回复
热议问题