Created
September 10, 2014 15:44
-
-
Save tehbeard/c807a39ddcd9d6195f10 to your computer and use it in GitHub Desktop.
improved youtube/vimeo video id parser.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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