Skip to content

Instantly share code, notes, and snippets.

@electronicbites
Last active March 24, 2020 19:49
Show Gist options
  • Save electronicbites/0bcbd450cd8108e9aa93255de2b44dae to your computer and use it in GitHub Desktop.
Save electronicbites/0bcbd450cd8108e9aa93255de2b44dae to your computer and use it in GitHub Desktop.
extract youtube id
function extractVideoID(url) {
let id = '';
url = url.replace(/(>|<)/gi,'').split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);
if ( url[2] !== undefined ) {
id = url[2].split(/[^0-9a-z_\-]/i)[0];
} else {
id = url;
}
return id;
}
// works with:
extractVideoID("https://www.youtube.com/watch?v=p_AyuhbnPOI&feature=youtu.be")
extractVideoID("https://www.youtube.com/watch?time_continue=3&v=CmMMpaUD3g8&feature=emb_logo")
extractVideoID("https://www.youtube.com/watch?v=SvCroUrKUiE&list=PLZ34CWzll19vq7f4y7fewR9IYRQ2sVm5r&index=26&t=0s")
extractVideoID("https://www.youtube.com/watch?v=Ns4wm6O9pYE&index=28")
extractVideoID("http://youtu.be/0zM3nApSvMg")
extractVideoID("http://www.youtube.com/embed/0zM3nApSvMg?rel=0")
// does not work with:
extractVideoID("http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment