【问题标题】:Regex to extract both video id or playlist id from youtube url正则表达式从 youtube url 中提取视频 ID 或播放列表 ID
【发布时间】:2015-11-24 12:55:09
【问题描述】:

我想知道如何使用单个正则表达式根据 url 提取 youtube 视频 ID 或播放列表 ID。正则表达式还应确保域是 youtube.com 以下是我需要的一些结果:

提取播放列表 ID

    https://www.youtube.com/playlist?list=PLuC2HflhhpLGQ4RgqA76_Gv52fGA0909r
    www.youtube.com/playlist?list=PLuC2HflhhpLGQ4RgqA76_Gv52fGA0909r
    http://www.youtube.com/playlist?list=PLuC2HflhhpLGQ4RgqA76_Gv52fGA0909r
    https://www.youtube.com/embed/videoseries?list=PLuC2HflhhpLGQ4RgqA76_Gv52fGA0909r  

提取视频 ID

https://www.youtube.com/watch?v=fqMfRi2gJok&index=1&list=PLuC2HflhhpLGQ4RgqA76_Gv52fGA0909r
https://www.youtube.com/watch?v=fqMfRi2gJok
http://youtu.be/cCnrX1w5luM 
http://youtube.com/embed/cCnrX1w5luM
http://youtube.com/v/cCnrX1w5luM
https://www.youtube.com/v/cCnrX1w5luM
www.youtube.com/v/cCnrX1w5luM
youtube.com/v/cCnrX1w5luM

这些只是示例网址。我需要为所有可能的 youtube 链接结构提取相应的 ID。

简而言之,提取视频 ID,如果不存在,则获取播放列表 ID。

【问题讨论】:

  • 我建议先捕获v,然后再捕获list。因为那样你就无法决定哪个字符串是哪个。
  • 单个正则表达式是什么意思?播放列表和视频 ID 以及所有形式?
  • @terces907 是的,我需要一个表达式来提取所有 url 结构
  • 主要是,你想要 vdo_id 对吗?但如果没有 vdo_id 你想要 playlist_id 吗?

标签: javascript regex youtube


【解决方案1】:

您的问题明显有两种模式

第一个:

^.*?(?:v|list)=(.*?)(?:&|$)

对于任何具有显式属性的 url,或者您可以说它们在 url 中有 = 符号。

说明

^.*?(?:v|list)=:任何字符串直到单词v=list=,在这里我们更喜欢v 而不是list

(.*?)(?:&|$):任何以& 符号或结束行符号$ 结尾的字符串,这里我们更喜欢& 而不是$

第二个:

^(?:(?!=).)*\/(.*)$

对于任何没有属性的url或者url中没有=符号。

说明

^(?:(?!=).)*\/: 任何没有= 符号的字符串(这里由负前瞻(?!=) 处理)直到/ 符号,

(.*)$: 直到行尾的任意字符串。

将它们组合成一个我们得到的正则表达式

^(?:https?:\/\/)?(?:www\.)?youtu\.?be(?:\.com)?.*?(?:v|list)=(.*?)(?:&|$)|^(?:https?:\/\/)?(?:www\.)?youtu\.?be(?:\.com)?(?:(?!=).)*\/(.*)$

这里,

添加(?:https?:\/\/)?(?:www\.)?youtu\.?be(?:\.com)?来处理www.youtube.com的各种形式的url

这应该可以帮助你得到你想要的

见:DEMO

重要提示:这个问题,提问者想从www.youtube.com 中提取id,他更喜欢“视频ID”而不是“播放列表ID”。

【讨论】:

  • 它还从 xyz.com/v/cCnrX1w5luM 中提取,这不是预期的。
  • 你应该事先告诉我。请将此要求添加到您的问题中。
  • @jollykoshy 此答案未能捕获列表:https://www.youtube.com/watch?v=fqMfRi2gJok&index=1&list=PLuC2HflhhpLGQ4RgqA76_Gv52fGA0909r
  • @terces907 您的未能捕获列表:https://www.youtube.com/watch?v=fqMfRi2gJok&index=1&list=PLuC2HflhhpLGQ4Rg‌​qA76_Gv52fGA0909r
  • @DanielCheung 他更喜欢video_id 而不是list_id,我已经在解释中提到了。
【解决方案2】:

这里是:

/\?(?:v|list)=(\w*)/g

您可以使用正则表达式或 (|)

您可以在这里测试并查看:

https://regex101.com/r/mI3qY9/2

更新

我更新了正则表达式(感谢您对捕获下划线的评论),并使第一组不捕获

更新也捕获:youtu.be/cCnrX1w5luM

/(?:\?v=|\?list=|be/)(\w)/g*

https://regex101.com/r/mI3qY9/6

【讨论】:

  • 您可以将第一个捕获组变为非捕获组。最好使用\w+ 而不是[a-zA-Z0-9]*
  • 下划线是列表字符串的一部分吗?因为您的正则表达式没有捕捉到这一点。
  • 我现在改变它是捕获它
  • 它不会为 youtu.be/cCnrX1w5luM 提取。很抱歉之前没有提及。
【解决方案3】:

https://regex101.com/r/mI3qY9/4

此正则表达式假定您为其提供了合法的 Youtube 链接。这会将所有vlists 一起抓取:

/(?:(?:\?|&)(?:v|list)=|embed\/|v\/|youtu\.be\/)((?!videoseries)[a-zA-Z0-9_]*)/g

细分:

/
(?:                         //non-capturing group
  (?:\?|&)(?:v|list)=       //? or & following a v or list
  |                         //or
  embed\/                   //embed/
  |                         //or
  v\/                       //v/            
  |                         //or
  youtu\.be\/               //youtu.be/
)
(
  (?!videoseries)           //will not capture "videoseries"
  [a-zA-Z0-9_]*             //capture any alphabet digits or underscore that follows afterwards
)          
/g                          //global

但是你可能分不清哪个是v,哪个是list,所以,

这只会抓取v:

/(?:(?:\?|&)v=|embed\/|v\/|youtu\.be\/)((?!videoseries)[a-zA-Z0-9_]*)/g

这只会抓取list:

/(?:(?:\?|&)list=)((?!videoseries)[a-zA-Z0-9_]*)/g

这只会抓取 YouTube vs:

/(?:youtube\.com.*(?:\?|&)(?:v)=|youtube\.com.*embed\/|youtube\.com.*v\/|youtu\.be\/)((?!videoseries)[a-zA-Z0-9_]*)/g

仅限 YouTube lists:

/(?:youtube\.com.*(?:\?|&)(?:list)=)((?!videoseries)[a-zA-Z0-9_]*)/g

这基本上是相同的,但在正则表达式中也添加了youtube\.com.*。它不会抓住例如http://example.com/v/abc

https://regex101.com/r/mI3qY9/5

说明:

youtube\.com.*          //Matches youtube.com and any multiple characters followed

【讨论】:

  • 正则表达式也匹配“videoseries”。如何避免这种情况?此外,正则表达式无法检查域是否是 youtube.com 本身。
  • 很抱歉不熟悉 Youtube 链接,“videoseries”不是视频吗?
  • youtube.com/embed/… 应该只产生 PLuC2HflhhpLGQ4RgqA76_Gv52fGA0909r
  • 除此 url 提取播放列表 id 之外的所有操作都已完成,其中视频 id 应为 youtube.com/…
  • @jollykoshy 我会看看的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多