Skip to content Skip to sidebar Skip to footer

How To Retrieve A Youtube Playlist Id Using Regex And Js

I'm trying to retrieve playlist ids from youtube links like: https://www.youtube.com/watch?v=hv_X327YUdI&list=SPGznEl712WelO6ZhS8lc2ssweLuQaCKld or https://www.youtube.com/pla

Solution 1:

This is how I ended up doing it:

This function validates that the link is from youtube:

functionyoutube_validate(url) {

        var regExp = /^(?:https?:\/\/)?(?:www\.)?youtube\.com(?:\S+)?$/;
        return url.match(regExp)&&url.match(regExp).length>0;

    }

This function retrieves the playlist id

//get playlist id from urlfunctionyoutube_playlist_parser(url){

        var reg = newRegExp("[&?]list=([a-z0-9_]+)","i");
        var match = reg.exec(url);

        if (match&&match[1].length>0&&youtube_validate(url)){
            return match[1];
        }else{
            return"nope";
        }

    }    

Solution 2:

Ok, i suppose you have already extracted the links:

var link = 'https://www.youtube.com/watch?v=hv_X327YUdI&list=SPGznEl712WelO6ZhS8lc2ssweLuQaCKld';

var reg = newRegExp("[&?]list=([a-z0-9_]+)","i");
var match = reg.exec(link);
alert(match[1]);

explanation

[&?]       oneof these characters
list=
(          capture group1
 [A-Za-z0-9_]+all characters that arein [A-Za-z0-9_], oneor more times
)          close capture group1

Solution 3:

(?:youtube.com.(?:\?|&)(?:list)=)((?!videoseries)[a-zA-Z0-9_-])

(group #1)

Some playlist id's have "-" https://regexr.com/3h5gs np:

https://www.youtube.com/watch?v=ldY6WNjEmGY&list=RDw9pC-51IQ60&index=6

Sorry for German techno :> first what i found

Solution 4:

get playlist id from url

functionplaylist_id(url) {
    varVID_REGEX = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/var regPlaylist = /[?&]list=([^#\&\?]+)/;
    var match = url.match(regPlaylist);
    return match[1];
}

get video id from playlist url

functionvideo_id_from_playlist(url) {
    varVID_REGEX = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/var video_id = url.match(VID_REGEX)[2];
    return video_id ;
}

or video id from url

functionget_video_id(url) {
    url = url.split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);
    return (url[2] !== undefined) ? url[2].split(/[^0-9a-z_\-]/i)[0] : url[0];
}

Post a Comment for "How To Retrieve A Youtube Playlist Id Using Regex And Js"