0

我正在尝试从 youtube 链接中检索播放列表 ID,例如:

https://www.youtube.com/watch?v=hv_X327YUdI&list=SPGznEl712WelO6ZhS8lc2ssweLuQaCKld

或者

https://www.youtube.com/playlist?list=SPGznEl712WelO6ZhS8lc2ssweLuQaCKld

并拒绝不属于 youtube 的链接。

所以在这种情况下,结果将是:

SPGznEl712WelO6ZhS8lc2ssweLuQaCKld
4

4 回答 4

3

这就是我最终这样做的方式:

此函数验证链接是否来自 youtube:

    function youtube_validate(url) {

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

    }

此函数检索播放列表 ID

  //get playlist id from url
    function youtube_playlist_parser(url){

        var reg = new RegExp("[&?]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";
        }

    }    
于 2013-06-02T00:44:26.640 回答
2

好的,我想您已经提取了链接:

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

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

解释

[&?]       one of these characters
list=
(          capture group 1
 [A-Za-z0-9_]+  all characters that are in [A-Za-z0-9_], one or more times
)          close capture group 1
于 2013-06-01T01:44:59.967 回答
0

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

(组#1)

一些播放列表 id 有“-” https://regexr.com/3h5gs np:

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

对不起德国技术:>首先我发现了什么

于 2017-11-11T12:01:46.290 回答
0

从 url 获取播放列表 ID

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

从播放列表 url 获取视频 ID

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

或来自 url 的视频 ID

function get_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];
}
于 2019-04-10T15:20:38.820 回答