From: Stefano Crocco Date: 2008-04-08T05:31:19+09:00 Subject: Re: Extract youtube video id with eval? On Monday 07 April 2008, comopasta Gr wrote: > Hi, > > I'm just doing the research but maybe someone already knows... > > I have a youtube video url: > http://www.youtube.com/watch?v=n1NVfDlU6yQ&feature=related > (&feature might or might not be present) > > I can get the thum of the video using: > http://img.youtube.com/vi/n1NVfDlU6yQ/default.jpg > > So, I just need to use the video id. > > Now, how to extract the video id easily? Eval? > Yeah simple I know...I'm working on it in the meanwhile. > > Thanks If you only need to extract the part of the url between watch?v= and either the end of the string or the &feature part, you can use a simple regexp: id = url.match(/watch\?v=(.*?)(?:&feature|\Z)/)[1] This matches the string 'watch?v= followed by any number of characters and by either the string &feature or the end of the string. The middle part (which I described as "any number of character") are then stored in the first subgroup (the element with index 1 of the MatchData object returned by match). You can create the image url with: img_url = "http://img.youtube.com/vi/#{id}/default.jpg" I hope this helps Stefano