使用 JavaScript 解析 Vimeo ID?

发布于 2024-09-03 06:41:08 字数 124 浏览 5 评论 0原文

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

鸠书 2024-09-10 06:41:08
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/*

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

This works for all valid Vimeo URLs which follows these patterns:

http://vimeo.com/*

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

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

煞人兵器 2024-09-10 06:41:08

由于 Vimeo 视频的 URL 由 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");
}

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");
}
无法回应 2024-09-10 06:41:08

如果您想先检查 Vimeo URL:

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

例如

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

返回

11918221

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

E.g.

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

returns

11918221
逆夏时光 2024-09-10 06:41:08

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

I think Matilda's is the best answer, but it's a non-working code draft, so merging it with Sean Kinsey's answer we get this working code version:

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");
}
放低过去 2024-09-10 06:41:08

如果您可以只解析像 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

If you are OK with only parsing a Vimeo URL like https://vimeo.com/407943692 you don't need regex. This is simpler imao:

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

However users often find links to other formats like these:

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

This is my solution that handles every case I have found:

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
}

Source:

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

纸短情长 2024-09-10 06:41:08

当然。

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

接下来您需要该 ID 号,对吗?假设它位于 URL 内,您也可以使用正则表达式(反向引用

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

Sure.

You should first check the validity/sanity of the URL with a regex and make sure it matches the pattern you expect. (More about regex'es here)

Next you need that ID number, right? Assuming that it's located within the URL, you can extract that also using a regex (backreference)

It's all just basically string and regex handling.

秋叶绚丽 2024-09-10 06:41:08
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"));
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"));
你丑哭了我 2024-09-10 06:41:08
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
    }
}
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
    }
}
故笙诉离歌 2024-09-10 06:41:08

我的 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;
  }

我的 Angular 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;
  }

My Javascript solution:

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

My typescript solution for angular 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;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文