Regex 使用正则表达式验证Youtube播放列表URL
如何使用正则表达式验证youtube播放列表 我已经从其他问题中找到了验证视频的答案。Regex 使用正则表达式验证Youtube播放列表URL,regex,url,Regex,Url,如何使用正则表达式验证youtube播放列表 我已经从其他问题中找到了验证视频的答案。 /^http:\/\/(?:www\)?youtube.com\/watch\(?=.*v=\w+(:\S+)$/ 但我无法验证这样的url: http://www.youtube.com/watch?list=PL1F9CA2A03CF286C2&v=pFS4zYWxzNA& 或 http://www.youtube.com/watch?v=pFS4zYWxzNA&list=PL1F9CA2A03CF286
/^http:\/\/(?:www\)?youtube.com\/watch\(?=.*v=\w+(:\S+)$/
但我无法验证这样的url:http://www.youtube.com/watch?list=PL1F9CA2A03CF286C2&v=pFS4zYWxzNA&
或
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
我最终得到了一个类似但不同的结果:
/^.*(youtu.be\/|list=)([^#\&\?]*).*/
针对以下URL工作:
var regExp = /^.*(youtu.be\/|list=)([^#\&\?]*).*/;
var match = url.match(regExp);
if (match && match[2]){
return match[2];
}
return null;
我的报税表是PLBOh8f9FoHHjOz0vGrD20WcTtJar LOrw是的,这个正则表达式有效。但是,最后一个捕获组将始终为空,因为每个备选方案末尾的
*
匹配字符串末尾的所有内容。此外,使用点是不合适的,因为它匹配的空格在任何URL中都无效。为了改进这一点,我将用:[^&\s]*
替换所有点星,否则,干得好。@ridgerunner:我确实尝试过将。*更改为[^&\s]*,但它不起作用。。我把它改成这个/^http:\/\/(?:www.)?youtube\.com\/watch\?((v=[^&\s]*&list=[^&\s]*)(list=[^&\s]*&v=[^&\s]*)(&[^&\s]*$/
@ridgerunner:我添加了最后一个组来处理你的额外参数,如&feature=list相关的&playnext=1
,另请参见编辑