Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
181
rated 0 times [  183] [ 2]  / answers: 1 / hits: 46332  / 13 Years ago, mon, august 1, 2011, 12:00:00

I've looked all over and I've seen many ways to parse the video ID off a URL for youtube, however, none of them have matched all the various formats the YouTube url could be in. I've tried messing with regex presented in the previous posts, but nothing seemed to work.



The closest post I found that covered all the various URL formats was this one: How do I find all YouTube video ids in a string using a regex?



However, this does not work for:
http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/FJUvudQsKCM



I'm doing this in Javascript. Can someone help?!



Thanx in advance.



Current URL formats and script I am using:



var url = http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/FJUvudQsKCM;
//var url = http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo;
//var url = http://youtu.be/NLqAF9hrVbY;
//var url = http://www.youtube.com/embed/NLqAF9hrVbY;
//var url = https://www.youtube.com/embed/NLqAF9hrVbY;
//var url = http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US;
//var url = http://www.youtube.com/watch?v=NLqAF9hrVbY;
//var url = http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo;
//var url = http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I;
//var url = http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo;
//var url = http://www.youtube.com/watch?v=JYArUl0TzhA&feature=featured;

var videoID = url.match(/(?:youtu.be/|youtube.com(?:/embed/|/v/|/watch?v=|/user/S+|/ytscreeningroom?v=))([w-]{10,12})b/)[1];
alert(videoID);

More From » regex

 Answers
22

This is a duplicate question, and has been answered before.



I think you will find the regexp there will also work here.



parse youtube video id using preg_match



EDIT:
I noticed that it does not work for the sandalsResort URL you have at the top of your list, so you can modify the regex to the following (converted for use in JS)



var myregexp = /(?:youtube.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu.be/)([^&?/s]{11})/gi;


All I did was to replace user with [^/]+



The ID is still captured in back-reference 1.


[#90877] Sunday, July 31, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eanskylerg

Total Points: 524
Total Questions: 107
Total Answers: 100

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
;