从Vimeo网址获取视频ID [英] Get video id from Vimeo url
本文介绍了从Vimeo网址获取视频ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试找到最佳的正则表达式来从网址获取vimeo视频ID。
I'm trying to find the best regexp to fetch vimeo video id from URL.
示例网址:
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/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
我当前的正则表达式不起作用:
My current regexp which doesn't work:
/http:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/
Playground&在此测试: http://jsbin.com/asuqic/1/edit?javascript,live
推荐答案
自2016年起。 @Martin Ender 表示不再最新
Since 2016. And @Martin Ender said no longer up to date
所以这是一个 API解决方案: (没有正则表达式,但API调用者和安全!)
jQuery:
function GetVimeoIDbyUrl(url) {
var id = false;
$.ajax({
url: 'https://vimeo.com/api/oembed.json?url='+url,
async: false,
success: function(response) {
if(response.video_id) {
id = response.video_id;
}
}
});
return id;
}
缩小:
function GetVimeoIDbyUrl(e){var i=!1;return $.ajax({url:"https://vimeo.com/api/oembed.json?url="+e,async:!1,success:function(e){e.video_id&&(i=e.video_id)}}),i}
Pure / Native JS: (IE9 +& Modern browsers )
function GetVimeoIDbyUrl(url) {
var id = false;
var request = new XMLHttpRequest();
request.open('GET', 'https://vimeo.com/api/oembed.json?url='+url , false);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var response = JSON.parse(request.responseText);
if(response.video_id) {
id = response.video_id;
}
}
};
request.send();
return id;
}
缩小:
function GetVimeoIDbyUrl(e){var t=!1,o=new XMLHttpRequest;return o.open("GET","https://vimeo.com/api/oembed.json?url="+e,!1),o.onload=function(){if(o.status>=200&&o.status<400){var e=JSON.parse(o.responseText);e.video_id&&(t=e.video_id)}},o.send(),t}
演示: https://jsbin.com/mevejoxudo/1/edit?js,output
某种类型网址不支持? : https://vimeo.com/help/contact#tech-api (告诉他们,别告诉我呵呵:D)
这篇关于从Vimeo网址获取视频ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文