by @kodeazy

jquery get youtube video ID from URL using split function?

Home » JQuery » jquery get youtube video ID from URL using split function?
  • To get the ID of the YouTube video from URL first we have to split the URL and get the substring by neglecting other parameters

    //var url="https://www.youtube.com/watch?v=tQ0yjYUFKAE";
    var url="https://www.youtube.com/watch?v=tQ0yjYUFKAE&list=TLGGoEoGU6P8wlUwNzA3MjAyMQ";
    var totalString=url.split("v=")[1];
    var longUrl=totalString.indexOf("&");
    if(longUrl==-1){
    console.log(totalString); 
    }
    else{
    console.log(totalString.substring(0,longUrl))
    }

    output

    tQ0yjYUFKAE