Extract parameter value from url using regular expressions

JavascriptRegexUrl

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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

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

Not tested but this should work:

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

Solution 4 - Javascript

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.

Solution 5 - Javascript

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 6 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOscarRyzView Question on Stackoverflow
Solution 1 - JavascriptCrescent FreshView Answer on Stackoverflow
Solution 2 - Javascriptmoha297View Answer on Stackoverflow
Solution 3 - JavascriptRobert SwisherView Answer on Stackoverflow
Solution 4 - Javascriptuser90843View Answer on Stackoverflow
Solution 5 - JavascriptTaha Ishfaq BhuttaView Answer on Stackoverflow
Solution 6 - JavascriptArun PanneerselvamView Answer on Stackoverflow