Extract parameter value from url using regular expressions

123,108

Solution 1

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.

Solution 2

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.

Solution 3

Not tested but this should work:

/\?v=([a-z0-9\-]+)\&?/i

Solution 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

Solution 5

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.

Share:
123,108
OscarRyz
Author by

OscarRyz

Software Developer who happens to like writing code. Here are some interesting answers you might like to upvote :") Why java people frequently consume exception silently ? Coding in Other (Spoken) Languages How to create an string from the contents of a file History of Objective-C square brackets (as I remember it) ( visible only to >10k users )

Updated on July 09, 2022

Comments

  • OscarRyz
    OscarRyz almost 2 years

    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...

  • Tadmas
    Tadmas almost 15 years
    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;
  • SavoryBytes
    SavoryBytes almost 15 years
    some youtube urls have a - as well, such as... youtube.com/watch?v=22hUHCr-Tos
  • Robert Swisher
    Robert Swisher almost 15 years
    @MyWhriledView Updated for the hyphen
  • soupagain
    soupagain over 14 years
    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})/
  • Admin
    Admin about 14 years
    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