Last active
December 24, 2017 14:35
-
-
Save kevinchisholm/c819119f8bc7bf1e3e81e93fb6b19588 to your computer and use it in GitHub Desktop.
Get the ID from a YouTube Video URL
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
//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