Parsing a Vimeo ID using JavaScript?

Tom picture Tom · May 26, 2010 · Viewed 20.4k times · Source

How do I parse an ID from a Vimeo URL in JavaScript?

The URL will be entered by a user, so I will need to check that they have entered it in the correct format.

I need the ID so that I can use their simple API to retrieve video data.

Answer

Sean Kinsey picture Sean Kinsey · May 26, 2010

As URLs for Vimeo videos are made up by http://vimeo.com/ followed by the numeric id, you could do the following

var url = "http://www.vimeo.com/7058755";
var regExp = /http:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/;

var match = url.match(regExp);

if (match){
    alert("id: " + match[2]);
}
else{
    alert("not a vimeo url");
}