Regex URL Path from URL


I am having a little bit of regex trouble.

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

If I use this regex /.+ it matches /video as well.

I would need some kind of anti / negative match to not include //

8/25/2015 11:05:36 AM

Accepted Answer

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


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


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

var url = require('url')
var youtubeUrl = ""

Which does all of the regex work for you.

  protocol: 'http:',
  slashes: true,
  auth: null,
  host: '',
  port: '80',
  hostname: '',
  hash: '#hello',
  search: '?docid=-7246927612831078230&hl=en',
  query: 'docid=-7246927612831078230&hl=en',
  pathname: '/videoplay',
  path: '/videoplay?docid=-7246927612831078230&hl=en',
  href: '' 
8/25/2015 11:06:21 AM

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 = "";

parser.protocol; // => "http:"
parser.hostname; // => ""
parser.port;     // => "3000"
parser.pathname; // => "/pathname/";   // => "?search=test"
parser.hash;     // => "#hash";     // => ""

Thank you John Long, you made by day!

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow