views:

41

answers:

2

Hi,

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
+3  A: 

Try this regex:

watch(?:\/|(?:\?|.*&)v=)(\w+)

The result will be in the 1st capture group.

Demo: http://rubular.com/r/7J9FSgwBMf

Aillyn
+1 Rock'n the Regex!
Tyler Egeto
Thanks a lot @Aillyn
Ela
A: 

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]);

**

Ela