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...
-
checkout also this question: stackoverflow.com/questions/738351/…dfa– dfa2009年08月14日 22:59:30 +00:00Commented Aug 14, 2009 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– OscarRyz2009年08月15日 00:35:03 +00:00Commented Aug 15, 2009 at 0:35
-
'/v=[0-9A-Za-z]*/' stackoverflow.com/questions/11706986/…bluesky– bluesky2015年06月30日 04:02:34 +00:00Commented Jun 30, 2015 at 4:02
-
Checkout the javascript module 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– radiovisual2016年04月12日 14:53:33 +00:00Commented Apr 12, 2016 at 14:53
7 Answers 7
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.
2 Comments
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.
Not tested but this should work:
/\?v=([a-z0-9\-]+)\&?/i
2 Comments
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.
3 Comments
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
Comments
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.
Comments
This RegEx supports many YouTube url formats.
/^(?:https?:\/\/)?(?:m\.|www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|shorts\/|watch\?.+&v=))((\w|-){11})(?:\S+)?$/
Supported url formats
const ytUrlFormats = {
normal: 'https://www.youtube.com/watch?v=12345678901',
share: 'https://youtu.be/12345678902',
shareWithStartTime: 'https://youtu.be/12345678903?t=6',
mobileBrowserUrl: 'https://m.youtube.com/watch?v=12345678904&list=RD12345678901&start_radio=1',
long: 'https://www.youtube.com/watch?v=12345678905&list=RD12345678905&start_radio=1&rv=smKgVuS',
longWithStartTime: 'https://www.youtube.com/watch?v=12345678906&list=RD12345678901&start_radio=1&rv=12345678901&t=38',
shorts: 'https://youtube.com/shorts/12345678907',
};
You can check this in the TS Playground (run & check in the console)