Get video id from Vimeo url

前端 未结 5 2002
失恋的感觉
失恋的感觉 2021-02-01 16:33

I\'m trying to find the best regexp to fetch vimeo video id from URL.

Example urls :

https://vimeo.com/11111111
http://vimeo.com/1111111         


        
5条回答
  •  暖寄归人
    2021-02-01 17:08

    Since 2016. And @Martin Ender said no longer up to date


    So here is a API solution : (without regexp but API caller and safe!)

    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;
    }
    

    Minify :

    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;
    }
    

    Minify :

    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}
    

    Demo : https://jsbin.com/mevejoxudo/1/edit?js,output

    Some type of url doesn't support ? : https://vimeo.com/help/contact#tech-api ( Tell them, dont tell me hehe :D )

提交回复
热议问题