【问题标题】:extract vimeo video id from the url从 url 中提取 vimeo 视频 id
【发布时间】:2015-05-29 11:06:20
【问题描述】:

虽然这个问题可以在 SO 上找到,但我遇到了一个小问题。我无法使用这里使用的 vimeo 正则表达式提取 vimeo id:Vimeo Regex

我现在使用的代码:

function vimeoProcess(url){
   var vimeoReg = /https?:\/\/(?:www\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?)/;
   var match = url.match(vimeoReg);
   if (match){
      console.log(match[3]);
   }else{
      return "<span class='error'>error</span>";
   }
}

它不会安慰任何东西。 有人可以帮忙吗?

【问题讨论】:

  • 你在调用那个函数吗?因为它有效:jsfiddle.net/mvx9Lkjm/1
  • 也许你的 url 字符串有空格或换行符?尝试.trim() url 字符串或将多行标志m 添加到您的正则表达式。
  • @JeremyThille 我正在使用https://player.vimeo.com/video/120911380 但是当我使用https://vimeo.com/channels/demoreels/120911380 它控制台Refused to display 'https://vimeo.com/channels/demoreels/120911380' in a frame because it set 'X-Frame-Options' to 'sameorigin'.
  • 正则表达式中不存在该语法。
  • @kmsdev 我该怎么做

标签: javascript jquery regex


【解决方案1】:

这个“神奇的正则表达式”并不那么神奇。例如,它不适用于您正在使用的 URL。

这是我刚刚设置的另一个解析器:

var urls =
[
  "https://vimeo.com/11111111",
  "http://vimeo.com/11111111",
  "https://www.vimeo.com/11111111",
  "http://www.vimeo.com/11111111",
  "https://vimeo.com/channels/11111111",
  "http://vimeo.com/channels/11111111",
  "https://vimeo.com/channels/mychannel/11111111",
  "http://vimeo.com/channels/yourchannel/11111111",
  "https://vimeo.com/groups/name/videos/11111111",
  "http://vimeo.com/groups/name/videos/11111111",
  "https://vimeo.com/album/2222222/video/11111111",
  "http://vimeo.com/album/2222222/video/11111111",
  "https://vimeo.com/11111111?param=test",
  "http://vimeo.com/11111111?param=test",
  "http://vimeo.com/whatever/somethingelse/11111111?param=test",
  "http://www.player.vimeo.com/stuff/otherstuff/11111111"
];

$.each(urls, function(index, url) {
    
  var firstPart = url.split('?')[0].split("/");
  var vid = firstPart[firstPart.length - 1];
      
  $("table").append('<tr><td>'+url+'</td><td><span>'+vid+'</span></td></tr>');
});
td   { font-family: monospace; }
span { background: lightgreen; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr><th>Input</th><th>Result</th></tr>
</table>

工作原理:

url.split('?')"http://vimeo.com/whatever/somethingelse/11111111?param=test" 分解为["http://vimeo.com/whatever/somethingelse/11111111","param=test"]

...如果 URL 中没有 ?,则直接使用 ["http://vimeo.com/whatever/somethingelse/11111111"]

现在[0] 获取数组的第一个元素,即"http://vimeo.com/whatever/somethingelse/11111111"

然后我们使用.split('/') 拆分它,得到["http:","","vimeo.com","whatever","somethingelse","11111111"]

现在我们只需要获取最后一个元素,即我们的视频 ID:

vid = firstPart[firstPart.length - 1] // Gives "11111111"

【讨论】:

  • 是的,我刚刚添加了一个测试 URL。
  • 感谢乔恩的改进 :)
【解决方案2】:

使用您当前的代码,只需将您的正则表达式更改为这个:

/https?:\/\/player\.vimeo\.com\/video\/(\d+)/gm

并使用第一个(唯一的)匹配元素$1

【讨论】:

    【解决方案3】:

    如果您只处理 vimeo url,您还可以观察并考虑其 url 中唯一的整数为您提供视频 id。

    var noquery = url.replace(/\?.*/,'') // remove anything after a ?
    var id = noquery.replace(/[^0-9]/g, '') // get all the digits
    

    这会给你一个更简单的代码,直到它中断

    这完全取决于您是想从您知道是视频 id 的 url 中提取 id,还是想从一组 url 中提取 vimeo 视频 url

    【讨论】:

      【解决方案4】:

      基于 Jeremy Thille 在列表中的回答。

      最后一步,我们能否使用pop()从该数组中获取最后一个元素,可能是这样的:

      let videoId = url.split('?')[0].split('/').pop()
      

      【讨论】:

        猜你喜欢
        • 2016-11-10
        • 2012-10-28
        • 1970-01-01
        • 2015-03-01
        • 2012-03-22
        • 1970-01-01
        • 2019-10-24
        • 2017-02-08
        • 2013-02-24
        相关资源
        最近更新 更多