Skip to content

Instantly share code, notes, and snippets.

@remoblaser
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remoblaser/7146f1d2af40fe929c96 to your computer and use it in GitHub Desktop.
Save remoblaser/7146f1d2af40fe929c96 to your computer and use it in GitHub Desktop.
Parse Youtube / Vimeo Video URL to iFrame
function getCode() {
var pattern1 = /(?:http?s?:\/\/)?(?:www\.)?(?:vimeo\.com)\/?(.+)/g;
var pattern2 = /(?:http?s?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g;
var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
var video = "https://vimeo.com/58821159";
if(pattern1.test(video)) {
var video_id = video.split('/');
var id = video_id[video_id.length -1];
return '<iframe width="100%" height="720" src="//player.vimeo.com/video/' + id + '" frameborder="0" webkitall owfullscreen mozallowfullscreen allowfullscreen></iframe>';
}
if(pattern2.test(video)){
var video_id = video.split('v=')[1];
var ampersandPosition = video_id.indexOf('&');
if(ampersandPosition != -1) {
video_id = video_id.substring(0, ampersandPosition);
}
return '<iframe width="100%" height="720" src="http://www.youtube.com/embed/' + video_id + ' frameborder="0" allowfullscreen></iframe>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment