Reputation: 3214
Any of you gone through this task? Please tell me a solution.
I have to extract video id alone from youtube url : http://www.youtube.com/watch?v=Ls8ppLu72NQ&feature=popular
From this i need only Ls8ppLu72NQ How can i extract it. I know i can use string replace but is there a way to
extract it easily with regex.
Url can be all these formats
http://www.youtube.com/watch?v=Ls8ppLu72NQ&feature=popular
http://www.youtube.com/watch?v=Ls8ppLu72NQ
http://www.youtube.com/watch/Ls8ppLu72NQ
Upvotes: 1
Views: 1002
Reputation: 3214
Thanks a lot @Aillyn
Its worked for me.
I made it in the following way **
var myPattern:RegExp = /watch(?:\/|(?:\?|.*&)v=)(\w+)/ig;
var str:String = "http://www.youtube.com/watch?&vx=123&v=Ls8ppLu72NT";
var result:Object = myPattern.exec(str);
trace(result[1]);
**
Upvotes: 0
Reputation: 23813
Try this regex:
watch(?:\/|(?:\?|.*&)v=)(\w+)
The result will be in the 1st capture group.
Demo: http://rubular.com/r/7J9FSgwBMf
Upvotes: 3