使用JavaScript解析Vimeo ID?

时间:2010-05-26 20:30:21

标签: javascript vimeo

如何在JavaScript中解析Vimeo URL中的ID?

网址将由用户输入,因此我需要检查他们是否以正确的格式输入了该网址。

我需要ID,以便我可以使用他们的简单API来检索视频数据。

9 个答案:

答案 0 :(得分:21)

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

答案 1 :(得分:16)

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

这适用于遵循以下模式的所有有效Vimeo URL:

http://vimeo.com/*

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

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

答案 2 :(得分:7)

如果您想先检查Vimeo网址:

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

E.g。

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

返回

11918221

答案 3 :(得分:2)

不确定

您应该首先使用正则表达式检查URL的有效性/健全性,并确保它与您期望的模式匹配。 (更多关于regex'es here

接下来你需要那个身份证号码,对吧?假设它位于URL中,您也可以使用正则表达式(backreference

来提取它

基本上只是字符串和正则表达式处理。

答案 4 :(得分:2)

var Vimeo =
{
    get_video_id: function(url)
    {
        var regExp = /http(s)?:\/\/(www\.)?vimeo.com\/(\d+)(\/)?(#.*)?/

        var match = url.match(regExp)

        if (match)
            return match[3]
    },

    get_video_url: function(id)
    {
        return 'https://vimeo.com/' + id
    }
}

答案 5 :(得分:2)

function getVimeoId(url) {
    var m = url.match(/^.+vimeo.com\/(.*\/)?([^#\?]*)/);
    return m ? m[2] || m[1] : null;
}

console.log(getVimeoId("http://vimeo.com/54178821"));
console.log(getVimeoId("http://vimeo.com/channels/nudiecutie/57383513"));

答案 6 :(得分:2)

我认为Matilda是最好的答案,但它是一份非工作代码草案,所以将它与Sean Kinsey的答案合并,我们得到了这个有效的代码版本:

var url = "http://www.vimeo.com/7058755"; //Or any other Vimeo url format
var regExp = /^.*(vimeo\.com\/)((channels\/[A-z]+\/)|(groups\/[A-z]+\/videos\/))?([0-9]+)/;

var match = url.match(regExp);

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

答案 7 :(得分:0)

我的Javascript解决方案:

function vimeo_parser(url){
    // var regExp = /http:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/;
    var regExp = /^.*(vimeo\.com\/)((channels\/[A-z]+\/)|(groups\/[A-z]+\/videos\/))?([0-9]+)/;
    var match = url.match(regExp);
    return (match&&match[5])? match[5] : false;
  }

我对8号角的打字稿解决方案:

  vimeo_parser(url){
    // var regExp = /http:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/;
    var regExp = /^.*(vimeo\.com\/)((channels\/[A-z]+\/)|(groups\/[A-z]+\/videos\/))?([0-9]+)/;
    var match = url.match(regExp);
    return (match&&match[5])? match[5] : false;
  }

答案 8 :(得分:0)

如果您可以只解析像 https://vimeo.com/407943692 这样的 Vimeo URL,则不需要正则表达式。这是更简单的imao:

let vimeoLink = "https://vimeo.com/407943692";
let url = new URL(vimeoLink);
//Remove leading /
let videoId = url.pathname.substring(1);

但是,用户经常会找到指向其他格式的链接,例如:

http://vimeo.com/423630
https://vimeo.com/1399176
http://vimeo.com/423630087
https://vimeo.com/423630087
https://player.vimeo.com/video/423630087
https://player.vimeo.com/video/423630087?title=0&byline=0&portrait=0
https://vimeo.com/channels/staffpicks/423630087
https://vimeo.com/showcase/7008490/video/407943692

这是我的解决方案,可以处理我发现的每个案例:

let regEx = /(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/?(showcase\/)*([0-9))([a-z]*\/)*([0-9]{6,11})[?]?.*/;
let match = mediaVideoLink.match(regEx);
if (match && match.length == 7) {
    let videoId = match[6];
}
else {
    //Handle error
}

来源:

https://stackoverflow.com/a/65845916/3850405