Extract parameter value from url using regular expressions

javascript, regex, url

Solution

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.

Problem

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

Original source

Related problems