1<----------------------------------- HTML ---------------------------------->
2YouTube ID: <span id="myId"></span>
3<br />
4<br />
5Embed code: <pre id="myCode"></pre>
6
7function getId(url) {
8 var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
9 var match = url.match(regExp);
10
11 if (match && match[2].length == 11) {
12 return match[2];
13 } else {
14 return 'error';
15 }
16}
17<----------------------------------- JS ---------------------------------->
18var myId = getId('http://www.youtube.com/watch?v=zbYf5_S7oJo');
19
20$('#myId').html(myId);
21
22$('#myCode').html('<iframe width="560" height="315" src="//www.youtube.com/embed/' + myId + '" frameborder="0" allowfullscreen></iframe>');
1<iframe width="560" height="315" src="https://www.youtube.com/embed/videoseries?list=PLQGSXoAiT2W4W8D3397nl4OB_GxfOmJk9" allowfullscreen></iframe>