Last active
November 3, 2020 10:53
-
-
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
This file contains hidden or 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
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