2011-08-01 39 views
16

我已经看了很多,我已经看到很多方法来解析视频ID的YouTube网址,但是,他们都没有匹配所有的YouTube网址可以在各种格式。已经尝试搞乱前一篇文章中提到的正则表达式,但似乎没有任何工作。用于YouTube的正则表达式ID

我发现的最接近的职位,涵盖所有不同的URL格式是这样的一个: http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/FJUvudQsKCM

我在Javascript中这样做:How do I find all YouTube video ids in a string using a regex?

然而,这不适用于工作。有人可以帮忙吗?

Thanx提前。

当前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); 
+0

的可能重复[使用Javascript REGEX:如何从URL获取YouTube视频ID?](http://stackoverflow.com/questions/3452546/javascript-regex-how-to-get-youtube-video-id-from-url) –

+0

你可以试试[获得视频-ID(https://github.com/radiovisual/get-video-id)。它会从任何已知的YouTube网址字符串(或嵌入字符串)中获取该ID。 – radiovisual

回答

43

这是一个重复的问题,并已答复过。

我想你会发现正则表达式也会在这里工作。

parse youtube video id using preg_match

编辑: 我发现它不适合你必须在你的列表顶部的sandalsResort URL工作,所以你可以修改正则表达式以下(换算为JS使用)

var myregexp = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i; 

我所做的只是与[^/]+

的ID,以取代user在后面-R仍然捕获eference 1.

+1

谢谢....如上所述,我发现了很多不同的正则表达式,但没有一个覆盖所有的URL可能性。这很好用! – Stanley

4

这将是非常混乱写一个正则表达式处理所有这些可能的URL。

我可能会使用一个if ... else if ... else结构来确定URL的形式,然后使用更小的更具体的正则表达式来提取每个视频ID。

+0

这正是[get-video-id]中发生的事情(https://github.com/radiovisual/get-video-id)。 – radiovisual

1

你可能不需要这个正则表达式。模式中的变化很小,分隔符本身(/,有时为?,=#)不变。我建议你采取这个步骤,使用普通的旧字符串操作来决定你的下一步行动:

  1. 拆分网址/
  2. 忽略http://www.(如果存在)。
  3. 检查域名是否为youtube.comyoutu.be
  4. 如果DN是youtu.be,则ID是下一个段。返回并停止。
  5. 开始解析参数。检查下一个细分受众群:
    • 如果是embed,请完整返回以下细分受众群。
    • 如果是v,则拆分?并返回第一部分。
    • 如果它是user,请提前计算四段,然后您将获得您的ID。
    • 如果是watch,则拆分?,然后拆分=

...等等。

我不知道YouTube网址有多少种可能的模式,但是如果您有完整的格式列表,则可以简单地在它们周围构建一个if/else树。我的主要建议是在/上拆分,并使用URL中的上下文提示来确定如何解析其余部分。

+0

这实质上是[get-video-id](https://github.com/radiovisual/get-video-id)中发生的事情。它并不试图为所有内容维护一个RegExp,而是将url模式分解为多个组,这使事情变得容易得多。 – radiovisual

0

试试这个:

var urls = 
["http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/FJUvudQsKCM", 
"http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo", 
"http://youtu.be/NLqAF9hrVbY", 
"http://www.youtube.com/embed/NLqAF9hrVbY", 
"https://www.youtube.com/embed/NLqAF9hrVbY", 
"http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US", 
"http://www.youtube.com/watch?v=NLqAF9hrVbY", 
"http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo", 
"http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I", 
"http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo", 
"http://www.youtube.com/watch?v=JYArUl0TzhA&feature=featured"]; 

var ids = []; 

for(var i in urls) { 
    tmp = urls [ i ]; 
    tmp2 = get_video_id(tmp); 
    if(tmp2 != null) 
    { 
     ids.push("url:" + tmp + " ID:" + tmp2); 
    } 
} 

alert(ids.join("\n")); 



function get_video_id(input) { 
return input.match(/(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/user\/\S+|\/ytscreeningroom\?v=|\/sandalsResorts#\w\/\w\/.*\/))([^\/&]{10,12})/)[1]; 
} 

输出:

url:http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/FJUvudQsKCM ID:FJUvudQsKCM 
url:http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo ID:p3vcRhsYGo 
url:http://youtu.be/NLqAF9hrVbY ID:NLqAF9hrVbY 
url:http://www.youtube.com/embed/NLqAF9hrVbY ID:NLqAF9hrVbY 
url:https://www.youtube.com/embed/NLqAF9hrVbY ID:NLqAF9hrVbY 
url:http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US ID:NLqAF9hrVbY? 
url:http://www.youtube.com/watch?v=NLqAF9hrVbY ID:NLqAF9hrVbY 
url:http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo ID:p3vcRhsYGo 
url:http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I ID:NRHVzbJVx8I 
url:http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo ID:p3vcRhsYGo 
url:http://www.youtube.com/watch?v=JYArUl0TzhA&feature=featured ID:JYArUl0TzhA 
+1

我觉得这样做的麻烦是,“sandalsResort”部分可能不是静态的,并且会因为具有相同格式的不同网址而发生变化。 – Benjam

+1

Scobleizer URL的ID缺少一个字符。 –

0
var get_id = function(url){ 
    var code = url.match(/v=([^&#]{5,})/) 
    return (typeof code[1] == 'string') ? code[1] : false; 
} 
3

我用这个正则表达式:/youtu(?:.*\/v\/|.*v\=|\.be\/)([A-Za-z0-9_\-]{11})/,它为我工作得很好。

-1

incorperated你的正则表达式这个例子:

(有没有真正得到一个数组出文本的(多个YouTube视频的方式)

复制并粘贴与文件名的文件:detectYoutubeLinksAsYouType .HTML

PS:不仅是我...还是stackoverflow.com完全瞎扯淡的登录功能...

<!DOCTYPE HTML> 
<html> 
    <head> 
     <title></title> 

     <!-- scripts --> 
     <!-- last jquery version that supports ie8/9 --> 
     <script type="text/javascript" src="../js/jquery-1.10.2.js"></script> 
     <script type="text/javascript"> 
      /* search for youtube-video-id inside a given text/url */ 
      function findYoutubeVideoID(url) { 

       // thanks for the regexes guys! 
       var YoutubeRegexObject_v1 = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i; // only gets the first VideoURL 
       var YoutubeRegexObject_v2 = /(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/user\/\S+|\/ytscreeningroom\?v=|\/sandalsResorts#\w\/\w\/.*\/))([^\/&]{10,12})/; 

       var YoutubeVideoID = url.match(YoutubeRegexObject_v1); 

       return YoutubeVideoID[1]; 
      } 

      /* generate youtube embed code */ 
      function generateYoutubeEmbedCode(YoutubeVideoID,width,height) 
      { 
       if(!width) 
       { 
        width = "854"; 
       } 
       if(!height) 
       { 
        height = "510"; 
       } 
       return '<iframe width="'+width+'" height="'+height+'" src="//www.youtube.com/embed/'+YoutubeVideoID+'" frameborder="0" allowfullscreen></iframe>'; 
      } 

      $(document).ready(function() { 
       $("#text").on('change keyup paste', function() { 
        var text = $(this).html(); 
        var YoutubeVideoID = findYoutubeVideoID(text); 
        var YoutubeVideoEmbedCode = generateYoutubeEmbedCode(YoutubeVideoID); 
        $("#findYoutubeVideoID").html(YoutubeVideoID); 
        $("#DisplayVideo").html(YoutubeVideoEmbedCode); 
       }); 

       $("#search").on('click', function() { 
        var text = $("#text").html(); 
        var YoutubeVideoID = findYoutubeVideoID(text); 
        var YoutubeVideoEmbedCode = generateYoutubeEmbedCode(YoutubeVideoID); 
        $("#findYoutubeVideoID").html(YoutubeVideoID); 
        $("#DisplayVideo").html(YoutubeVideoEmbedCode); 
       }); 
      }); 
     </script> 
    </head> 
    <body> 
     <style> 
      .parent { 
       margin: 0 auto; 
       position: relative; 
       border: 1px solid red; 
       width: 500px; 
      } 
      .element { 
       border: 1px solid red; 
       position: relative; 
       float: left; 
       min-height: 20px; 
       margin: 10px; 
       min-width: 45%; 
      } 
     </style> 
     <div class="parent"> 
      <div class="element">Detect youtube links as you type!</div> 
      <div class="element" id="text" contenteditable="true"> 
       Copy paste Youtube-Video-Url here! e.g. this one: https://www.youtube.com/watch?v=QOJ1nYPBonQ 
      </div> 
      <div class="element" >The VideoID is:</div> 
      <div class="element" id="findYoutubeVideoID"></div> 
      <div class="element" id="DisplayVideo"></div> 
      <div class="element"> <button id="search">Search for YoutubeID</button></div> 
     </div> 
    </body> 
</html> 
+0

为什么我需要输入一个验证码但记住了? – canoodle