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

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

问题描述

我已经创建了一个从JavaScript或Vimeo获取URL的函数(在JavaScript中)。它计算出该特定视频的提供者和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网址?我已经尝试了很多,到目前为止似乎工作得很好。

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),但前提是你在网址表上的归纳是正确的,正则表达式可以合并为一个如下:

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,则第1和第2场比赛,如果你是第3和第4场比赛),所以你只需要处理。

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将会在第一场和第二场比赛中表现得非常好。

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

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

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