Parsing a Vimeo ID using JavaScript?

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.


由于Vimeo视频的网址由http://vimeo.com/和数字ID组成,因此您可以执行以下操作

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

regExp = /^.*(vimeo.com/)((channels/[A-z]+/)|(groups/[A-z]+/videos/))?([0-9]+)/
parseUrl = regExp.exec url
return parseUrl[5]

这适用于所有符合以下模式的有效Vimeo网址:

http://vimeo.com/*

http://vimeo.com/channels/*/*

http://vimeo.com/groups/*/videos/*


If you want to check for Vimeo URL first:

function getVimeoId( url ) {

  // Look for a string with 'vimeo', then whatever, then a
  // forward slash and a group of digits.
  var match = /vimeo.*/(d+)/i.exec( url );

  // If the match isn't null (i.e. it matched)
  if ( match ) {
    // The grouped/matched digits from the regex
    return match[1];
  }
}

Eg

getVimeoId('http://vimeo.com/11918221');

returns

11918221
链接地址: http://www.djcxy.com/p/28924.html

上一篇: 这是一个很好的哈希密码函数在PHP中? 如果不是,为什么不呢?

下一篇: 使用JavaScript解析Vimeo ID?