32

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

Example 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/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 & Test here : http://jsbin.com/asuqic/1/edit?javascript,live

Alexey Malev
  • 6,408
  • 4
  • 34
  • 52
l2aelba
  • 21,591
  • 22
  • 102
  • 138
  • 1
    you could use `url.split('/').slice(-1)[0].split('?')[0]`, or is regex a requirement? – Yoshi Nov 08 '12 at 10:17

5 Answers5

44

Update: I notice that this answer is getting some attention every now and then (through votes or comments). The answer is more than two years old, and likely the URL types supported are no longer up to date. I will not actively maintain this regex - it is merely intended as an answer to the question and hence only takes care of the formats listed there. Use this at your own risk, or - even better - use it merely as a starting point to develop your own regex based on an up-to-date and comprehensive list of URL formats.

See @l2aelba's answer for API solution https://stackoverflow.com/a/37695721/622813


This would be the full regex, which also ensures that the format is correct:

/https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)/

You can now retrieve the group name in capturing group 1 (if present), the album ID in capturing group 2 (if present) and the video ID in capturing group 3 (always).

Demo

Community
  • 1
  • 1
Martin Ender
  • 43,427
  • 11
  • 90
  • 130
  • some error ? `Line 1: vimeo_Reg = /https?:\/\/(?:www\.)?vimeo.com\/(?:channels\/|groups\/[^\/]*\/videos\/|album\/\d+\/video\/|)(\d+)($|\/|?)/; --- Unescaped '?'.` – l2aelba Nov 08 '12 at 10:16
  • 1
    @l2aelba sorry, copy paste mistake. copy the code from my answer into the demo. – Martin Ender Nov 08 '12 at 10:18
  • can you edit your demo link to `jsbin.com/asuqic/11/edit?javascript,live` ? i think your answer will be accepted :D – l2aelba Nov 08 '12 at 10:23
  • 1
    @l2aelba then I guess I had the wrong link in my answer again :D ... anyway, all settled now ;) – Martin Ender Nov 08 '12 at 10:27
  • I came across a link that goes vimeo.com/channels/channelname/video_id (probably because this thread is a bit old) which isn't covered by your result. I changed it up to /https?:\/\/(?:www\.)?vimeo.com\/(?:channels\/\w*\/*|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?)/; But then I get the wrong result for "http://www.vimeo.com/11111111". Maybe you can see right away what I am doing wrong haha – Aurelin Jul 02 '14 at 22:05
  • @Aurelin I think you wanted `channels\/(?:\w+\/)?` in order to ensure that there is another `/` after the channel name parameter. I'll edit the post, and finally fix that typo. – Martin Ender Jul 02 '14 at 22:13
  • Ah yes thank you! What is the ?: for, if you don't mind explaining? – Aurelin Jul 03 '14 at 16:09
  • 1
    @Aurelin [it suppresses capturing](http://www.regular-expressions.info/brackets.html) – Martin Ender Jul 03 '14 at 16:10
  • Added one more case "https://vimeo.com/ondemand/santaquest/114026446" Live: http://jsbin.com/taxugalalu/1/ – Raghav Dec 18 '14 at 11:25
  • I'm not expert in javascript regex and I can't support this kind of urls "http://vimeo.com/user18322218/review/66338120/7678a41439" can you handle it? – Kareem Elshahawy Feb 25 '15 at 08:27
  • Is there such a thing for youtube? – Gary Willoughby Apr 08 '15 at 13:50
  • It would be nice to retrieve [playback starting time](https://vimeo.com/help/faq/sharing-videos/share-features#can-i-link-viewers-to-a-specific-part-of-my-video) with a capturing group 4. For instance https://vimeo.com/11111111#t=1m2s. – ForguesR Jun 07 '16 at 18:50
12

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 )

l2aelba
  • 21,591
  • 22
  • 102
  • 138
4
var r = /(videos|video|channels|\.com)\/([\d]+)/,
    a = "https://vimeo.com/album/2222222/video/11111111";

console.log(a.match(r)[2]); // 11111111;

http://jsbin.com/asuqic/7/edit

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
3

The following regex will return the id on group 1 for those who only want to retrieve this information. It works with all the example URLs given in the question. It also works without http:// and https://. It also works with URLs like player.vimeo.com/...

/^.*(?:vimeo.com)\\/(?:channels\\/|groups\\/[^\\/]*\\/videos\\/|album\\/\\d+\\/video\\/|video\\/|)(\\d+)(?:$|\\/|\\?)/
service-paradis
  • 3,333
  • 4
  • 34
  • 43
  • 1
    Here is the unescaped version + added support for http://vimeo.com/channels/channelname/1111111111: `/^.*(?:vimeo.com)\/(?:channels\/|channels\/\w+\/|groups\/[^\/]*\/videos\/|album\/\d+\/video\/|video\/|)(\d+)(?:$|\/|\?)/` – Didier Ghys May 01 '15 at 09:38
0

For php, below code works for following urls

i.e. :

http://vimeo.com/88888888
http://player.vimeo.com/video/88888888
http://vimeo.com/channels/staffpicks/88888888
http://vimeo.com/groups/groupsname/88888888

PHP Code :

    $vimeoUrl = 'http://vimeo.com/channels/channelsName/11111111';
    $fetchVimeoIdArr = explode('/', $vimeoUrl);
    $idCounter = count($fetchVimeoIdArr) - 1;
    $vimeoId = $fetchVimeoIdArr[$idCounter];
Niko Jojo
  • 1,204
  • 2
  • 14
  • 31