Skip to content

Instantly share code, notes, and snippets.

@rutger1140
Created June 10, 2016 15:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rutger1140/ff3d25fe00956787a42220533f94511b to your computer and use it in GitHub Desktop.
Save rutger1140/ff3d25fe00956787a42220533f94511b to your computer and use it in GitHub Desktop.
Parse video url for YouTube and Vimeo - finds video ID
// Source: http://stackoverflow.com/a/22763925
function parseVideo(url) {
// - Supported YouTube URL formats:
// - http://www.youtube.com/watch?v=My2FRPA3Gf8
// - http://youtu.be/My2FRPA3Gf8
// - https://youtube.googleapis.com/v/My2FRPA3Gf8
// - Supported Vimeo URL formats:
// - http://vimeo.com/25451551
// - http://player.vimeo.com/video/25451551
// - Also supports relative URLs:
// - //player.vimeo.com/video/25451551
url.match(/(http:|https:|)\/\/(player.|www.)?(vimeo\.com|youtu(be\.com|\.be|be\.googleapis\.com))\/(video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9._%-]*)(\&\S+)?/);
if (RegExp.$3.indexOf('youtu') > -1) {
var type = 'youtube';
} else if (RegExp.$3.indexOf('vimeo') > -1) {
var type = 'vimeo';
}
return {
type: type,
id: RegExp.$6
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment