I\'m using this to convert youtube url to embed url.
text(t).html().replace(/(?:http:\\/\\/)?(?:www\\.)?(?:youtube\\.com)\\/(?:watch\\?v=)?(.+)/g, \'
function popYouTubeId(buttonid) {
var youTubeUrl = $(buttonid).attr('data-url');
var youTubeId;
var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
var match = youTubeUrl.match(regExp);
if (match && match[2].length == 11) {
youTubeId = match[2];
} else {
youTubeId = 'no video found';
}
$('#ytvideo').html('');
$('a.closex').click( function(){
$('.youtubepopup').remove();
});
}
var buttonid;
$('.videobutton').click( function(){
buttonid = '#'+$(this).attr('id');
popYouTubeId(buttonid);
});
Some elaboration on isherwood's demo for your consideration. Simplifies, and packs more into the function for multiple use.
jsfiddle