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
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 :
Minify :
Pure/Native JS : (IE9+ & Modern browsers)
Minify :
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 )
http://jsbin.com/asuqic/7/edit
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:
You can now retrieve the group name in capturing group
1
(if present), the album ID in capturing group2
(if present) and the video ID in capturing group3
(always).Demo
For php, below code works for following urls
i.e. :
PHP Code :
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://
andhttps://
. It also works with URLs likeplayer.vimeo.com/...