0

If given the following link to a YouTube video:

https://youtu.be/PqkaBUmJpq8?list=PLmmPGQQTKzSZSPd3pa6q9UQ-PkeCx1fam

Is it possible to extract the video id from it that gets used in the YouTube iFrame API ?

  • Possible duplicate of [Get Substring between two characters using javascript](http://stackoverflow.com/questions/14867835/get-substring-between-two-characters-using-javascript) – Linda Lawton - DaImTo May 10 '17 at 11:51

2 Answers2

0

Just ended up writing this function:

function extractVideoIdFromYouTubeUrl (url) {

    var stepOne = url.split('?')[0];
    var stepTwo = stepOne.split('/');
    var videoId = stepTwo[stepTwo.length-1];

    return videoId;

}
0
function extractVideoIdFromYouTubeUrl(url) {
  let id = url.substring(url.lastIndexOf("v=")+2,url.length);
  return id;
}

This solution assumes that the video ID is at the end of the URL. I would only advise using it for simple situations like where I needed it, where the user can't enter a URL, and the Youtube Player API creates a limited possible set of URLs.