Skip to content

Instantly share code, notes, and snippets.

@tehbeard
Created September 10, 2014 15:44
Show Gist options
  • Save tehbeard/c807a39ddcd9d6195f10 to your computer and use it in GitHub Desktop.
Save tehbeard/c807a39ddcd9d6195f10 to your computer and use it in GitHub Desktop.
improved youtube/vimeo video id parser.
// Improved variant of http://stackoverflow.com/questions/5612602/improving-regex-for-parsing-youtube-vimeo-urls/22763925#22763925
function parseVideoId(url){
var res = 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+)?/);
var type = null;
if(res[1].indexOf('youtu') > -1){
type = 'youtube';
} else if(res[1].indexOf('vimeo') > -1){
type = 'vimeo';
}
return {
type: type,
id: res[2]
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment