使用正则表达式验证 Youtube 播放列表 URL [英] Validating Youtube Playlist URL using Regex

查看:39
本文介绍了使用正则表达式验证 Youtube 播放列表 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用正则表达式验证 youtube 播放列表 网址?
我从其他问题中找到了验证视频的答案..
/^http:\/\/(?:www\.)?youtube.com\/watch\?(?=.*v=\w+)(?:\S+)?$/

How to validate a youtube playlist url using regex?
I've found the answer for validating video from other question..
/^http:\/\/(?:www\.)?youtube.com\/watch\?(?=.*v=\w+)(?:\S+)?$/

但我只是无法验证这样的网址:
http://www.youtube.com/watch?list=PL1F9CA2A03CF286C2&v=pFS4zYWxzNA&

http://www.youtube.com/watch?v=pFS4zYWxzNA&list=PL1F9CA2A03CF286C2&

But I've just unable to validate a url like this :
http://www.youtube.com/watch?list=PL1F9CA2A03CF286C2&v=pFS4zYWxzNA&
or
http://www.youtube.com/watch?v=pFS4zYWxzNA&list=PL1F9CA2A03CF286C2&

推荐答案

试试这个

^(https|http):\/\/(?:www\.)?youtube\.com\/watch\? #you forgot to mask the dot before com
(                                         #may lead to wrong parsing
    (v=.*&list=.*)| #v and then list
    (list=.*&v=.*)  #or versa verse - feel free to use better matching
)                   #for ids like "pFS4zYWxzNA"
(&.*)*$             #all other kinds of parameter

我改进了匹配

^(https|http):\/\/(?:www\.)?youtube\.com\/watch\?
(?:&.*)*                         #extra params at the beginning
(
    (?:
         v=([a-zA-Z0-9_\-]{11})     #propper mathing for id
         (?:&.*)*                #extras
         &list=([a-zA-Z0-9_\-]{18}) #list
    )
    | 
    (?:
         list=([a-zA-Z0-9_\-]{18})
         (?:&.*)*                #versa
         &v=([a-zA-Z0-9_\-]{11})
    )
)
(?:&.*)*                         #extras at the end
(?:\#.*)*$                       #anchors

这篇关于使用正则表达式验证 Youtube 播放列表 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆