Extract youtube video id with eval?

C

comopasta Gr

Hi,

I'm just doing the research but maybe someone already knows...

I have a youtube video url:
(&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
 
C

comopasta Gr

Hmm. Just read some stuff about eval. The video url is initially
provided by users. So is it wise to use eval for that (?)
 
C

comopasta Gr

Looks like what I'm looking for is an equivalent to javascripts
toQueryParams().
Is there one?

Cheers
 
S

Stefano Crocco

Hi,

I'm just doing the research but maybe someone already knows...

I have a youtube video url:
(&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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top