我想要得到v=id从YouTube的URL与JavaScript(没有jQuery,纯JavaScript)。

YouTube URL格式示例

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

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

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

这些格式的结果

u8nQa1cJyX8


当前回答

/^https?:\/\/(?:(?:youtu\.be\/)|(?:(?:www\.)?youtube\.com\/(?:(?:watch\?(?:[^&]+&)?vi?=)|(?:vi?\/)|(?:shorts\/))))([a-zA-Z0-9_-]{11,})/i

下面是一个优化的正则表达式,它可以找到视频id,并准确地遵循YouTube oEmbed对embed url的定义。您可以在这里看到我与测试url的匹配:https://regex101.com/r/q4mWg1/1

它故意不匹配协议相对url(//而不是https://)和youtu-nocookie.com url,因为这些不在oEmbed定义中,从而降低了性能。

你可以在这里查看oEmbed规范: https://oembed.com/

官方提供商的定义,包括YouTube的定义,在这里:https://oembed.com/providers.json

我发现这在Wordpress网站上非常有用,我需要在帖子内容中匹配oEmbed url。

其他回答

我对“jeffreypriebe”提供的Regex做了一个增强,因为他需要一种YouTube URL是视频的URL,当他们通过一个频道看。

不,但这是我武装的函数。

<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

试试这个——

function getYouTubeIdFromURL($url) 
{
  $pattern = '/(?:youtube.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu.be/)([^"&?/ ]{11})/i';
  preg_match($pattern, $url, $matches);

  return isset($matches[1]) ? $matches[1] : false;
}

在c#中,它看起来是这样的:

public static string GetYouTubeId(string url) {
    var regex = @"(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?|watch)\/|.*[?&amp;]v=)|youtu\.be\/)([^""&amp;?\/ ]{11})";

    var match = Regex.Match(url, regex);

    if (match.Success)
    {
        return match.Groups[1].Value;
    }

    return url;
  }

请随意修改。

Python3版本:

import re

def get_youtube_id(url):
   match = re.match('^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))?\?v?=?(?P<id>\w*).*', url);
   return match.group('id')

如果你想在shell/bash/zsh/fish脚本中包含它,下面是如何做到的:

echo -n "$YOUTUBE_URL" | python -c "import re; import sys; m = re.match('^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))?\?v?=?(?P<id>\w*).*', sys.stdin.read()); sys.stdout.write(m.group('id'))"

例子:

echo -n "https://www.youtube.com/watch/?v=APYVWYHS654" | python -c "import re; import sys; m = re.match('^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))?\?v?=?(?P<id>\w*).*', sys.stdin.read()); sys.stdout.write(m.group('id'))"  
APYVWYHS654
var video_url = document.getElementById('youtubediv').value;
        if(video_url!=""){
        ytid(video_url);
        document.getElementById("youtube").setAttribute("src","http://www.youtube.com/embed/"+ytid(video_url));
        }
        function ytid(video_url){
            var video_id = video_url.split('v=')[1];
            var ampersandPosition = video_id.indexOf('&');
            if(ampersandPosition != -1) {
                video_id = video_id.substring(0, ampersandPosition);
            }
            return video_id;
        }

我希望它能有所帮助