如何修改正则表达式以解析来自 YouTube URL 的参数 [英] How to modify a regular expression to parse a parameter from a YouTube URL
本文介绍了如何修改正则表达式以解析来自 YouTube URL 的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个正则表达式:
/^.*((youtu.be/)|(v/)|(/u/w/)|(embed/)|(watch?))??v?=?([^#&?]*).*/;
它将解析来自 YouTube URL 的视频 ID,例如
It will parse the video id from a YouTube URL such as
http://www.youtube.com/看?v=6ciG_6Y3rD0&feature=youtu.be&t=6m40s
或
http://youtu.be/6ciG_6Y3rD0?t=6m40s
我想修改它,让它也解析t参数.
I want to modify it so that it also parses the t parameter.
有什么想法吗?
谢谢,豪
推荐答案
Capture YouTube video ID (and time seek parameter t
如果存在)
Capture YouTube video ID (and time seek parameter t
if exists)
正则表达式:
^(?:https?://)?(?:www.)?(?:youtu.be/|youtube.com/(?:embed/|v/|watch?v=))([w-]{10,12})(?:[&?#].*?)*?(?:[&?#]t=([dhm]+s))?$
PS:您会注意到我改进了您的原始 RegEx 以安全地验证 YouTube 链接
PS: you'll note I improved your original RegEx to validate the YouTube link safely
这篇关于如何修改正则表达式以解析来自 YouTube URL 的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文