Skip to content

Instantly share code, notes, and snippets.

@kevinchisholm
Last active December 24, 2017 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinchisholm/c819119f8bc7bf1e3e81e93fb6b19588 to your computer and use it in GitHub Desktop.
Save kevinchisholm/c819119f8bc7bf1e3e81e93fb6b19588 to your computer and use it in GitHub Desktop.
Get the ID from a YouTube Video URL
//set variables
var videoId = '',
youTubeUrlBase = 'https://www.youtube.com/watch?v=',
youTubeUrl = 'https://www.youtube.com/watch?v=JMP3MUwoEmw';
//returns the youtube ID from a URL
function getYoutubeId (url){
var myregexp = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i,
retArr = url.match(myregexp);
return retArr[1].split('&')[0];
};
//get the ID of the youTubeUrl
videoId = getId(youTubeUrl);
//log the ID for demonstration purposes
console.warn(videoId);
//open the you tube video in a new window
window.open(youTubeUrlBase + videoId);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment