54

This should be very simple (when you know the answer). From this question

I want to give the posted solution a try. My question is:

How to get the parameter value of a given URL using JavaScript regular expressions?

I have:

http://www.youtube.com/watch?v=Ahg6qcgoay4

I need:

Ahg6qcgoay4

I tried:

http://www.youtube.com/watch\\?v=(w{11})

But: I suck...

Community
  • 1
  • 1
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • checkout also this question: http://stackoverflow.com/questions/738351/recommendation-for-javascript-url-manipulation-library-api – dfa Aug 14 '09 at 22:59
  • @dfa: I might need that in the future, thanks for the link. I guess I should probably get this regexp right first :) – OscarRyz Aug 15 '09 at 00:35
  • '/v=[0-9A-Za-z]*/' http://stackoverflow.com/questions/11706986/php-getting-url-variable-with-preg-match – bluesky Jun 30 '15 at 04:02
  • Checkout the javascript module [get-video-id](https://github.com/radiovisual/get-video-id) that will extract the Youtube id from any known Youtube url format (including embed strings). It doesn't use one monolithic regex, but it employs a few regex's to find the different patterns. – radiovisual Apr 12 '16 at 14:53

6 Answers6

74

You almost had it, just need to escape special regex chars:

regex = /http\:\/\/www\.youtube\.com\/watch\?v=([\w-]{11})/;

url = 'http://www.youtube.com/watch?v=Ahg6qcgoay4';
id = url.match(regex)[1]; // id = 'Ahg6qcgoay4'

Edit: Fix for regex by soupagain.

Community
  • 1
  • 1
Crescent Fresh
  • 115,249
  • 25
  • 154
  • 140
  • 3
    Should probably also put in a test for if the match fails, such as var m = url.match(regex); id = (m && m.length > 1) ? m[1] : null; – Tadmas Aug 14 '09 at 22:55
  • 3
    Don't know how to edit answers, but the above answer is incorrect, as it fails with video IDs containing the - dash character. Therefore the regex should be: /http\:\/\/www\.youtube\.com\/watch\?v=([\w-]{11})/ – soupagain Feb 11 '10 at 08:57
22

Why dont you take the string and split it

Example on the url

var url = "http://www.youtube.com/watch?p=DB852818BF378DAC&v=1q-k-uN73Gk"

you can do a split as

var params = url.split("?")[1].split("&");

You will get array of strings with params as name value pairs with "=" as the delimiter.

moha297
  • 1,902
  • 1
  • 16
  • 16
  • that is a original idea, +1 for that, but I recommend using HttpUtility.ParseQueryString if you can live with referencing System.Web.dll, and not reinvent the wheel –  May 10 '10 at 10:15
5

Not tested but this should work:

/\?v=([a-z0-9\-]+)\&?/i
Robert Swisher
  • 1,300
  • 11
  • 12
5

v is a query parameter, technically you need to consider cases ala: http://www.youtube.com/watch?p=DB852818BF378DAC&v=1q-k-uN73Gk

In .NET I would recommend to use System.Web.HttpUtility.ParseQueryString

HttpUtility.ParseQueryString(url)["v"];

And you don't even need to check the key, as it will return null if the key is not in the collection.

  • 1
    Strictly speaking this should be var playlist = HttpUtility.ParseQueryString(new Uri(url).Query)["v"]; because ParseQueryString is looking for a query string, not a whole URL. – kingdango Jan 25 '13 at 19:32
  • Also this: http://stackoverflow.com/questions/20268544/portable-class-library-pcl-version-of-httputility-parsequerystring – Mikhail Orlov Sep 07 '15 at 11:33
  • 1
    Why is this answer in .NET when the OP has tagged JS? – Kevin Dec 20 '19 at 05:19
4

I know the question is Old and already answered but this can also be a solution

\b[\w-]+$

and I checked these two URLs

http://www.youtube.com/watch?v=Ahg6qcgoay4
https://www.youtube.com/watch?v=22hUHCr-Tos

DEMO

1

I use seperate custom functions which gets all URL Parameters and URL parts . For URL parameters, (which is the final part of an URI String, http://domain.tld/urlpart/?x=a&y=b

    function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
    }

The above function will return an array consisting of url variables.

For URL Parts or functions, (which is http://domain.tld/urlpart/?x=a&y=b I use a simple uri split,

function getUrlParams() { 
    var vars = {};
    var parts = window.location.href.split('/' );
    return parts;
}

You can even combine them both to be able to use with a single call in a page or in javascript.

Arun Panneerselvam
  • 2,263
  • 1
  • 17
  • 24