正则表达式从youtube / vimeo网址中提取域名和视频ID [英] Regex to extract domain and video id from youtube/vimeo url

查看:95
本文介绍了正则表达式从youtube / vimeo网址中提取域名和视频ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在复制一个函数,该函数将使用youtube / vimeo网址并返回视频来自的网站(vimeo / yt)以及视频ID。

I am copying a function that will take a youtube/vimeo url and return what site the video came from (vimeo/yt) as well as the video id.

这是我到目前为止所拥有的: http://jsfiddle.net/csjwf/181/

Here's what I have so far: http://jsfiddle.net/csjwf/181/

<strong>Result:</strong>
<div id="result"></div>





function parseVideoURL(url) {

    url.match(/^http:\/\/(?:.*?)\.?(youtube|vimeo)\.com\/(watch\?[^#]*v=(\w+)|(\d+)).+$/);
    return {
        provider : RegExp.$1,
        id : RegExp.$1 == 'vimeo' ? RegExp.$2 : RegExp.$3
    }
}

var result = document.getElementById("result");
var video = parseVideoURL("http://www.youtube.com/watch?v=PQLnmdOthmA&feature=feedrec_grec_index");
result.innerHTML = "Provider: " + video.provider + "<br>ID: " + video.id;

var video = parseVideoURL("http://vimeo.com/22080133");

result.innerHTML += "<br>--<br>Provider: " + video.provider + "<br>ID: " + video.id;

输出:

Result:
Provider: youtube
ID: PQLnmdOthmA
--
Provider: vimeo
ID: 2208013

但是,请注意vimeo vids的方式,如果url以ID结尾,则最后一个数字始终被截断。如果在vimeo url的末尾添加斜杠,则会完全拉出id。

However, notice how for vimeo vids, if the url ends in the ID, the last number is always cut off. If you add a slash to the end of the vimeo url the id is pulled fully.

推荐答案

。+ $ 最后要求至少一个最后一个数字后面的字符,作为一串数字捕获。这样可以剔除被捕获的数字。你有那个原因吗?

The .+$ at the end is requiring at least one character after the last digit that is captured as a string of digits. That will chop one digit off what is captured. Is there a reason you have that there?

你可以将最后的 + 更改为 * 喜欢这样:

You can change the last + to a * like this:

/^http:\/\/(?:.*?)\.?(youtube|vimeo)\.com\/(watch\?[^#]*v=(\w+)|(\d+)).*$/

甚至更好,完全摆脱最终部分,因为它看起来不像是需要的:

or even better, get rid of the end part entirely since it doesn't look like it's needed:

/^http:\/\/(?:.*?)\.?(youtube|vimeo)\.com\/(watch\?[^#]*v=(\w+)|(\d+))/

这里有一个更安全的方式来编写你的函数,它允许在youtube URL中查询参数的任何顺序,并且不会将东西放入不需要存在的正则表达式中。代码更长,但它更强大,并且更容易添加更多提供者:

Here's a bit safer way to write your function that allows for any order of the query parameters in the youtube URL and doesn't put stuff into the regex that doesn't need to be there. The code is longer, but it's much more robust and would be much easier to add more providers:

function parseVideoURL(url) {

    function getParm(url, base) {
        var re = new RegExp("(\\?|&)" + base + "\\=([^&]*)(&|$)");
        var matches = url.match(re);
        if (matches) {
            return(matches[2]);
        } else {
            return("");
        }
    }

    var retVal = {};
    var matches;

    if (url.indexOf("youtube.com/watch") != -1) {
        retVal.provider = "youtube";
        retVal.id = getParm(url, "v");
    } else if (matches = url.match(/vimeo.com\/(\d+)/)) {
        retVal.provider = "vimeo";
        retVal.id = matches[1];
    }
    return(retVal);
}

此处的工作版本: http://jsfiddle.net/jfriend00/N2hPj/

这篇关于正则表达式从youtube / vimeo网址中提取域名和视频ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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