如何在javascript中解析Dailymotion视频网址

时间:2022-09-15 15:44:24

I want to parse Dailymotion video url to get video id in javascript, like below:

我想解析Dailymotion视频网址以获取javascript中的视频ID,如下所示:

http://www.dailymotion.com/video/x44lvd

video id: "x44lvd"

视频ID:“x44lvd”

i think i need regex string to get a video id for all dailymotion video url combinations.

我想我需要正则表达式字符串来获取所有dailymotion视频网址组合的视频ID。

i found url parser regex for YouTube links, its working well like below:

我找到了YouTube链接的url解析器正则表达式,它的工作原理如下:

var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
    var match = url.match(regExp);
    var videoID = "";
    if (match && match[7].length == 11){
        videoID = match[7];
    }else
       alert('video not found');

can anybody please give me some advice about dailymotion?

有人可以给我一些关于dailymotion的建议吗?

4 个解决方案

#1


13  

function getDailyMotionId(url) {
    var m = url.match(/^.+dailymotion.com\/(video|hub)\/([^_]+)[^#]*(#video=([^_&]+))?/);
    if (m !== null) {
        if(m[4] !== undefined) {
            return m[4];
        }
        return m[2];
    }
    return null;
}

console.log(getDailyMotionId("http://www.dailymotion.com/video/x44lvd_rates-of-exchange-like-a-renegade_music"));
console.log(getDailyMotionId("http://www.dailymotion.com/video/x44lvd"));
console.log(getDailyMotionId("http://www.dailymotion.com/hub/x9q_Galatasaray"));
console.log(getDailyMotionId("http://www.dailymotion.com/hub/x9q_Galatasaray#video=xjw21s"));
console.log(getDailyMotionId("http://www.dailymotion.com/video/xn1bi0_hakan-yukur-klip_sport"));

I learned some regex in the meantime.

在此期间我学到了一些正则表达式。

This is an updated version which returns an array of any dailymotion id found in a text:

这是一个更新版本,它返回在文本中找到的任何dailymotion id的数组:

function getDailyMotionIds(str) {
    var ret = [];
    var re = /(?:dailymotion\.com(?:\/video|\/hub)|dai\.ly)\/([0-9a-z]+)(?:[\-_0-9a-zA-Z]+#video=([a-z0-9]+))?/g;     
    var m;

    while ((m = re.exec(str)) != null) {
        if (m.index === re.lastIndex) {
            re.lastIndex++;
        }
        ret.push(m[2]?m[2]:m[1]);
    }
    return ret;
}

test it here http://jsfiddle.net/18upkjaa/embedded/result/ by typing into the textbox

通过在文本框中输入来测试http://jsfiddle.net/18upkjaa/embedded/result/

#2


3  

Here is a little update to handle non /video/ or /hub/ path like the last one:

这是一个小的更新,以处理非/视频/或/ hub /路径,如上一个:

function getDailyMotionId(url) {
    var m = url.match(/^.+dailymotion.com\/((video|hub)\/([^_]+))?[^#]*(#video=([^_&]+))?/);
    return m ? m[5] || m[3] : null;
}

console.log(getDailyMotionId("http://www.dailymotion.com/video/x44lvd_rates-of-exchange-like-a-renegade_music"));
console.log(getDailyMotionId("http://www.dailymotion.com/video/x44lvd"));
console.log(getDailyMotionId("http://www.dailymotion.com/hub/x9q_Galatasaray"));
console.log(getDailyMotionId("http://www.dailymotion.com/hub/x9q_Galatasaray#video=xjw21s"));
console.log(getDailyMotionId("http://www.dailymotion.com/video/xn1bi0_hakan-yukur-klip_sport"));

// non /video/ or /hub/ url:
console.log(getDailyMotionId("http://www.dailymotion.com/fr/relevance/search/gangnam+style/1#video=xsbwie"));

#3


0  

Others answer doesn't match all the dailymotion urls

其他答案与所有​​dailymotion网址不匹配

(like http://dai.ly/x2no31b)

Use this regex :

使用这个正则表达式:

^(?:(?:http|https):\/\/)?(?:www.)?(dailymotion\.com|dai\.ly)\/((video\/([^_]+))|(hub\/([^_]+)|([^\/_]+)))$

#4


0  

I use this one (which covers short URLS like dai.ly/{videoId}):

我使用这个(包括像dai.ly/{videoId这样的简短URL):

^(?:(?:https?):)?(?:\/\/)?(?:www\.)?(?:(?:dailymotion\.com(?:\/embed)?\/video)|dai\.ly)\/([a-zA-Z0-9]+)(?:_[\w_-]+)?$

Debuggex Demo

#1


13  

function getDailyMotionId(url) {
    var m = url.match(/^.+dailymotion.com\/(video|hub)\/([^_]+)[^#]*(#video=([^_&]+))?/);
    if (m !== null) {
        if(m[4] !== undefined) {
            return m[4];
        }
        return m[2];
    }
    return null;
}

console.log(getDailyMotionId("http://www.dailymotion.com/video/x44lvd_rates-of-exchange-like-a-renegade_music"));
console.log(getDailyMotionId("http://www.dailymotion.com/video/x44lvd"));
console.log(getDailyMotionId("http://www.dailymotion.com/hub/x9q_Galatasaray"));
console.log(getDailyMotionId("http://www.dailymotion.com/hub/x9q_Galatasaray#video=xjw21s"));
console.log(getDailyMotionId("http://www.dailymotion.com/video/xn1bi0_hakan-yukur-klip_sport"));

I learned some regex in the meantime.

在此期间我学到了一些正则表达式。

This is an updated version which returns an array of any dailymotion id found in a text:

这是一个更新版本,它返回在文本中找到的任何dailymotion id的数组:

function getDailyMotionIds(str) {
    var ret = [];
    var re = /(?:dailymotion\.com(?:\/video|\/hub)|dai\.ly)\/([0-9a-z]+)(?:[\-_0-9a-zA-Z]+#video=([a-z0-9]+))?/g;     
    var m;

    while ((m = re.exec(str)) != null) {
        if (m.index === re.lastIndex) {
            re.lastIndex++;
        }
        ret.push(m[2]?m[2]:m[1]);
    }
    return ret;
}

test it here http://jsfiddle.net/18upkjaa/embedded/result/ by typing into the textbox

通过在文本框中输入来测试http://jsfiddle.net/18upkjaa/embedded/result/

#2


3  

Here is a little update to handle non /video/ or /hub/ path like the last one:

这是一个小的更新,以处理非/视频/或/ hub /路径,如上一个:

function getDailyMotionId(url) {
    var m = url.match(/^.+dailymotion.com\/((video|hub)\/([^_]+))?[^#]*(#video=([^_&]+))?/);
    return m ? m[5] || m[3] : null;
}

console.log(getDailyMotionId("http://www.dailymotion.com/video/x44lvd_rates-of-exchange-like-a-renegade_music"));
console.log(getDailyMotionId("http://www.dailymotion.com/video/x44lvd"));
console.log(getDailyMotionId("http://www.dailymotion.com/hub/x9q_Galatasaray"));
console.log(getDailyMotionId("http://www.dailymotion.com/hub/x9q_Galatasaray#video=xjw21s"));
console.log(getDailyMotionId("http://www.dailymotion.com/video/xn1bi0_hakan-yukur-klip_sport"));

// non /video/ or /hub/ url:
console.log(getDailyMotionId("http://www.dailymotion.com/fr/relevance/search/gangnam+style/1#video=xsbwie"));

#3


0  

Others answer doesn't match all the dailymotion urls

其他答案与所有​​dailymotion网址不匹配

(like http://dai.ly/x2no31b)

Use this regex :

使用这个正则表达式:

^(?:(?:http|https):\/\/)?(?:www.)?(dailymotion\.com|dai\.ly)\/((video\/([^_]+))|(hub\/([^_]+)|([^\/_]+)))$

#4


0  

I use this one (which covers short URLS like dai.ly/{videoId}):

我使用这个(包括像dai.ly/{videoId这样的简短URL):

^(?:(?:https?):)?(?:\/\/)?(?:www\.)?(?:(?:dailymotion\.com(?:\/embed)?\/video)|dai\.ly)\/([a-zA-Z0-9]+)(?:_[\w_-]+)?$

Debuggex Demo