0

Trying to turn youtube links to embeded, but my jquery doesnt seem to grab the video ids from the links

My JQuery:

$('.a').html(function(i, html) {
return html.replace(/(?:http:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g,'<iframe width="150" height="150" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>');
});

My HTML:

<div class="a">http://www.youtube.com/watch?v=w_yudtBvhCsc</div>
<div class="a">http://www.youtube.com/watch?v=w_yudtBvhCsd</div>

Im clueless...

Jeft Lebowski
  • 71
  • 2
  • 10

1 Answers1

1

You can just use split..

$('.a').html(function(i,v){
    var id = v.split('watch?v=')[1]; // get the id so you can add to iframe
    return '<iframe width="150" height="150" src="http://www.youtube.com/embed/' + id + '" frameborder="0" allowfullscreen></iframe>';
});

http://jsfiddle.net/nzvYv/

wirey00
  • 33,517
  • 7
  • 54
  • 65