YouTube ID 的正则表达式
我已经查看了所有内容,并且看到了很多从 YouTube 网址中解析视频 ID 的方法,但是,它们都没有匹配 YouTube 网址可能采用的所有各种格式。我尝试过使用所提供的正则表达式在之前的帖子中,但似乎没有任何效果。
我发现涵盖所有各种 URL 格式的最接近的帖子是这个: 如何使用正则表达式查找字符串中的所有 YouTube 视频 ID?
但是,这不适用于: http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B /0/FJUvudQsKCM
我正在这样做在 JavaScript 中。有人可以帮忙吗?
提前致谢。
我正在使用的当前 URL 格式和脚本:
var url = "http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/FJUvudQsKCM";
//var url = "http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo";
//var url = "http://youtu.be/NLqAF9hrVbY";
//var url = "http://www.youtube.com/embed/NLqAF9hrVbY";
//var url = "https://www.youtube.com/embed/NLqAF9hrVbY";
//var url = "http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US";
//var url = "http://www.youtube.com/watch?v=NLqAF9hrVbY";
//var url = "http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo";
//var url = "http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I";
//var url = "http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo";
//var url = "http://www.youtube.com/watch?v=JYArUl0TzhA&feature=featured";
var videoID = url.match(/(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/user\/\S+|\/ytscreeningroom\?v=))([\w\-]{10,12})\b/)[1];
alert(videoID);
I've looked all over and I've seen many ways to parse the video ID off a URL for youtube, however, none of them have matched all the various formats the YouTube url could be in. I've tried messing with regex presented in the previous posts, but nothing seemed to work.
The closest post I found that covered all the various URL formats was this one: How do I find all YouTube video ids in a string using a regex?
However, this does not work for:
http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/FJUvudQsKCM
I'm doing this in Javascript. Can someone help?!
Thanx in advance.
Current URL formats and script I am using:
var url = "http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/FJUvudQsKCM";
//var url = "http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo";
//var url = "http://youtu.be/NLqAF9hrVbY";
//var url = "http://www.youtube.com/embed/NLqAF9hrVbY";
//var url = "https://www.youtube.com/embed/NLqAF9hrVbY";
//var url = "http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US";
//var url = "http://www.youtube.com/watch?v=NLqAF9hrVbY";
//var url = "http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo";
//var url = "http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I";
//var url = "http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo";
//var url = "http://www.youtube.com/watch?v=JYArUl0TzhA&feature=featured";
var videoID = url.match(/(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/user\/\S+|\/ytscreeningroom\?v=))([\w\-]{10,12})\b/)[1];
alert(videoID);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是一个重复的问题,之前已经回答过。
我想你会发现那里的正则表达式在这里也适用。
使用 preg_match 解析 YouTube 视频 ID
编辑:
我注意到它不适用于您列表顶部的 sandalsResort URL,因此您可以将正则表达式修改为以下内容(转换为在 JS 中使用)
我所做的就是替换
user
和[^/]+
的ID 仍然在反向引用 1 中捕获。
This is a duplicate question, and has been answered before.
I think you will find the regexp there will also work here.
parse youtube video id using preg_match
EDIT:
I noticed that it does not work for the sandalsResort URL you have at the top of your list, so you can modify the regex to the following (converted for use in JS)
All I did was to replace
user
with[^/]+
The ID is still captured in back-reference 1.
我使用这个正则表达式:
/youtu(?:.*\/v\/|.*v\=|\.be\/)([A-Za-z0-9_\-]{11})/
对我来说效果很好。I use this regexp:
/youtu(?:.*\/v\/|.*v\=|\.be\/)([A-Za-z0-9_\-]{11})/
and it works fine for me.编写一个处理所有这些可能的 URL 的正则表达式会非常混乱。
我可能会使用 if ... else if ... else 结构来确定 url 的形式,然后使用更小、更具体的正则表达式来提取每个视频 ID。
It would be very messy to write a single regular expression which handles all of these possible URLs.
I would probably use an if ... else if ... else structure to determine which form the url is in and then use a smaller more specific regular expression to pull out each video ID.
您可能不需要正则表达式。模式和分隔符本身(
/
,有时是?
、=
或#) 不变。我建议您分步骤进行,使用普通的旧字符串操作来决定下一步行动:
/
上拆分 URL。http://
和www.
(如果存在)。youtube.com
或youtu.be
。youtu.be
,则 ID 是下一个分段。返回并停止。嵌入
,则完整返回以下片段。v
,则按?
进行拆分并返回第一部分。用户
,请数前面的四个段,您就会得到您的 ID。watch
,请先按?
拆分,然后再按=
拆分。...ETC。
我不知道 YouTube 网址有多少种可能的模式,但如果您有完整的格式列表,您可以简单地围绕它们构建一个 if/else 树。我的主要建议是拆分
/
并从那里开始,使用 URL 中的上下文提示来确定如何解析其余部分。You probably don't need a regex for this. There is very little variance in the pattern, and the delimiters themselves (
/
, and sometimes?
,=
, or#
) are unchanging. I recommend you take this in steps, using plain old string manipulation to decide your next move:/
.http://
andwww.
, if present.youtube.com
oryoutu.be
.youtu.be
, the ID is the next segment. Return it and stop.embed
, return the following segment in full.v
, split on?
and return the first part.user
, count four segments ahead and you'll have your ID.watch
, split on?
and then on=
....etc.
I don't know how many possible patterns there are for YouTube URLs, but if you have the full list of formats, you can simply build up an if/else tree around them. My main advice is just to split on
/
and go from there, using contextual hints in the URL to determine how to parse the rest of it.试试这个:
输出:
Try this:
Output:
将您的正则表达式合并到此示例中:(
实际上是否有一种方法可以从文本中获取数组(包含多个 youtube 视频?)
复制并粘贴一个文件名为: detectorYoutubeLinksAsYouType.html
ps:只有我...还是stackoverflow.com 的登录功能完全是胡说八道……
incorperated your regexes into this example:
(is there actually a way of getting an array out of a text (with multiple youtube videos?)
copy and paste it a file with filename: detectYoutubeLinksAsYouType.html
ps: Is it only me... or is the LOGIN functionality of stackoverflow.com complete bullsh...