改进用于解析 YouTube/Vimeo URL 的正则表达式 [英] Improving regex for parsing YouTube / Vimeo URLs

查看:39
本文介绍了改进用于解析 YouTube/Vimeo URL 的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个函数(在 JavaScript 中),它从 YouTube 或 Vimeo 获取一个 URL.它计算出该特定视频的提供者和 ID(演示:http://jsfiddle.net/csjwf/).

I've made a function (in JavaScript) that takes an URL from either YouTube or Vimeo. It figures out the provider and ID for that particular video (demo: http://jsfiddle.net/csjwf/).

function parseVideoURL(url) {

    var provider = url.match(/http://(:?www.)?(w*)/)[2],
        id;

    if(provider == "youtube") {

        id = url.match(/http://(?:www.)?(w*).com/.*v=(w*)/)[2];
    } else if (provider == "vimeo") {

        id = url.match(/http://(?:www.)?(w*).com/(d*)/)[2];
    } else {
        throw new Error("parseVideoURL() takes a YouTube or Vimeo URL");    
    }
    return {
        provider : provider,
        id : id
    }
}

它有效,但是作为正则表达式新手,我正在寻找改进它的方法.我正在处理的输入通常如下所示:

It works, however as a regex Novice, I'm looking for ways to improve it. The input I'm dealing with, typically looks like this:

http://vimeo.com/(id)
http://youtube.com/watch?v=(id)&blahblahblah.....

1) 现在我正在做三场单独的比赛,尝试用一个单一的表达做所有事情是否有意义?如果是,怎么办?

1) Right now I'm doing three separate matches, would it make sense to try and do everything in one single expression? If so, how?

2) 现有的匹配可以更简洁吗?它们是否不必要地复杂?或者可能不够?

2) Could the existing matches be more concise? Are they unnecessarily complex? or perhaps insufficient?

3) 是否有无法解析的 YouTube 或 Vimeo URL?我已经尝试了很多,到目前为止它似乎工作得很好.

3) Are there any YouTube or Vimeo URL's that would fail being parsed? I've tried quite a few and so far it seems to work pretty well.

总结:我只是在寻找改进上述功能的方法.非常感谢任何建议.

To summarize: I'm simply looking for ways improve the above function. Any advice is greatly appreciated.

推荐答案

我不确定你的问题 3),但只要你对 url 表单的归纳是正确的,正则表达式可以合并为一个,如下所示:

I am not sure about your question 3), but provided that your induction on the url forms is correct, the regexes can be combined into one as follows:

/http://(?:www.)?(?:(vimeo).com/(.*)|(youtube).com/watch?v=(.*?)&)/

您将在不同位置获得比赛(如果 vimeo 是第一和第二场比赛,如果是 youtube,则是第三和第四场比赛),所以您只需要处理.

You will get the match under different positions (1st and 2nd matches if vimeo, 3rd and 4th matches if youtube), so you just need to handle that.

或者,如果您非常确定 vimeo 的 id 只包含数字,那么您可以这样做:

Or, if you are quite sure that vimeo's id only includes numbers, then you can do:

/http://(?:www.)?(vimeo|youtube).com/(?:watch?v=)?(.*?)(?:z|&)/

提供者和 ID 将分别出现在第 1 次和第 2 次匹配中.

and the provider and the id will apprear under 1st and 2nd match, respcetively.

这篇关于改进用于解析 YouTube/Vimeo URL 的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆