youtube id from ulr
by Евгений
HTML
<div id="result"></div>
JavaScript
var urls = [
'http://www.youtube.com/watch?v=9MNvVgtUOig',
'http://youtu.be/9MNvVgtUOig',
'http://www.youtube.com/watch?feature=player_detailpage&v=9MNvVgtUOig',
'http://www.youtube.com/watch?feature=player_detailpage&v=9MNvVgtUOig#t=147',
'www.youtube.com/embed/9MNvVgtUOig',
'youtube.com/embed/9MNvVgtUOig'
],
iframes = [];
urls.forEach(function(item) {
iframes.push('<iframe width="560" height="315" src="//www.youtube.com/embed/' + matchYoutubeUrl(item) + '" frameborder="0" allowfullscreen></iframe>');
});
document.querySelector('#result').innerHTML = iframes.join('<br>');
function matchYoutubeUrl(url){
var p = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;
return (url.match(p)) ? RegExp.$1 : false ;
}