Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Parse Dailymotion Video Url in javascript

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

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

video id: "x44lvd"

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

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

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?

like image 814
relower Avatar asked Dec 04 '22 02:12

relower


1 Answers

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:

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

like image 145
Roman Avatar answered Dec 19 '22 04:12

Roman