Gli ID dei video di YouTube rappresentano la parte più rilevante di un URL di un video di YouTube. La procedura per ottenerli non è complessa.
Possiamo implementare la seguente funzione di utility:
function getYouTubeVideoID( url ) {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match( regExp );
if ( match && match[7].length == 11 ) {
return match[7];
} else {
console.warn( url + " is not a valid YouTube URL" );
}
}
Esempio d'uso:
var ytURL = "https://www.youtube.com/embed/coIsvOMYEi0?rel=0";
var ytID = getYouTubeVideoID( ytURL ); // coIsvOMYEi0