如何从 URL 获取 YouTube 视频 ID? [英] How do I get the YouTube video ID from a URL?

查看:44
本文介绍了如何从 URL 获取 YouTube 视频 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 JavaScript(无 jQuery,纯 JavaScript)从 YouTube 的 URL 中获取 v=id.

I want to get the v=id from YouTube’s URL with JavaScript (no jQuery, pure JavaScript).

http://www.youtube.com/watch?v=u8nQa1cJyX8&a=GxdCwVVULXctT2lYDEPllDR0LRTutYfW

http://www.youtube.com/watch?v=u8nQa1cJyX8

或者任何其他在 URL 中包含视频 ID 的 YouTube 格式.

u8nQa1cJyX8

推荐答案

我对jeffreypriebe"提供的 Regex 进行了增强,因为他需要一种 YouTube URL,即视频在浏览频道时的 URL.

I made an enhancement to Regex provided by "jeffreypriebe" because he needed a kind of YouTube URL is the URL of the videos when they are looking through a channel.

好吧,不,但这是我已装备的功能.

Well no but this is the function that I have armed.

<script type="text/javascript">
function youtube_parser(url){
    var regExp = /^.*((youtu.be/)|(v/)|(/u/w/)|(embed/)|(watch?))??v?=?([^#&?]*).*/;
    var match = url.match(regExp);
    return (match&&match[7].length==11)? match[7] : false;
}
</script>

这些是支持的 URL 类型

http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index
http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o
http://www.youtube.com/v/0zM3nApSvMg?fs=1&amp;hl=en_US&amp;rel=0
http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s
http://www.youtube.com/embed/0zM3nApSvMg?rel=0
http://www.youtube.com/watch?v=0zM3nApSvMg
http://youtu.be/0zM3nApSvMg

可以在 [http:///web.archive.org/web/20160926134334/]http://lasnv.net/foro/839/Javascript_parsear_URL_de_YouTube

这篇关于如何从 URL 获取 YouTube 视频 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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