Skip to content

Instantly share code, notes, and snippets.

@c3ry5
Last active November 3, 2020 10:53
Show Gist options
  • Save c3ry5/4c1c9929505462ed90e2 to your computer and use it in GitHub Desktop.
Save c3ry5/4c1c9929505462ed90e2 to your computer and use it in GitHub Desktop.
Regex Map/loop for getting the video id from youtube, vimeo, vine and instagram
getVideoId = {
siteMap : {
"youtube" : {
r : /(youtu\.be\/|youtube\.com\/(watch\?(.*&)?v=|(embed|v)\/))([^\?&"'>]+)/,
id : 5
},
"vimeo" : {
r : /https?:\/\/(?:www\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?)/,
id : 3
},
"vine" : {
r : /^http(?:s?):\/\/(?:www\.)?vine\.co\/v\/([a-zA-Z0-9]{1,13})$/,
id : 1
},
"instagram" : {
r : /https?:\/\/[w\.]*instagram\.[^\/]*\/([^?]*)\/([a-zA-Z0-9]{1,10})/,
id : 2
}
},
getData : function(url) {
var data,
arr,
site;
/** https://gist.github.com/c3ry5/b02bdf2dabe57772839a - checkUrl function**/
if(checkUrl(url) === true) {
for (site in this.siteMap) {
if (data) {
break;
}
arr = url.match(this.siteMap[site].r);
if (arr) {
data = {
source: site,
id: arr[this.siteMap[site].id]
};
}
}
if (data) {
return data;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment