使用 JavaScript 解析 Vimeo ID?
如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这适用于遵循以下模式的所有有效 Vimeo URL:
http://vimeo.com/*
http://vimeo.com/channels/*/*
http://vimeo.com/groups/*/videos/*
This works for all valid Vimeo URLs which follows these patterns:
http://vimeo.com/*
http://vimeo.com/channels/*/*
http://vimeo.com/groups/*/videos/*
由于 Vimeo 视频的 URL 由
http://vimeo.com/
后跟数字 id 组成,您可以执行以下操作As URLs for Vimeo videos are made up by
http://vimeo.com/
followed by the numeric id, you could do the following如果您想先检查 Vimeo URL:
例如
返回
If you want to check for Vimeo URL first:
E.g.
returns
我认为 Matilda 的答案是最好的答案,但它是一个不起作用的代码草案,因此将其与 Sean Kinsey 的答案合并,我们得到了这个工作代码版本:
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:
如果您可以只解析像
https://vimeo.com/407943692
这样的 Vimeo URL,则不需要正则表达式。这更简单imao:但是用户经常找到像这样的其他格式的链接:
这是我的解决方案,处理我发现的每种情况:
来源:
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:However users often find links to other formats like these:
This is my solution that handles every case I have found:
Source:
https://stackoverflow.com/a/65845916/3850405
当然。
您应该首先使用正则表达式检查 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.
我的 Javascript 解决方案:
我的 Angular 8 打字稿解决方案:
My Javascript solution:
My typescript solution for angular 8: