4

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?

relower
  • 1,293
  • 1
  • 10
  • 20

5 Answers5

14
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

Roman
  • 5,888
  • 26
  • 47
  • is this will work for all dailymotion url combinations? for example there is an url like http://www.dailymotion.com/hub/x9q_Galatasaray will this reg exp parse the videoid ? – relower Sep 12 '12 at 12:39
  • there is another one like this: http://www.dailymotion.com/video/xn1bi0_hakan-yukur-klip_sport i'm so sorry for writing url combinations step by step. – relower Sep 12 '12 at 12:53
  • dailymotion now has the `http://dai.ly/x44lvd` format, so this might not cover all cases. Will try to update the answer when I can. – reafle Jan 28 '15 at 10:11
  • 1
    Should be updated to work with links containing embed, example: dailymotion.com/embed/video/xq2cmn – Amr May 13 '16 at 22:01
4

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

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"));
jbdemonte
  • 667
  • 1
  • 7
  • 15
  • 1
    Don't work with links containing embed, example: http://www.dailymotion.com/embed/video/xq2cmn – Amr May 12 '16 at 17:55
  • 1
    just use var m = url.match(/^.+dailymotion.com\/embed\/(video|hub)\/([^_]+)[^#]*(#video=([^_&]+))?/); – TomInCode Dec 12 '20 at 14:00
0

Others answer doesn't match all the dailymotion urls

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

Use this regex :

^(?:(?:http|https):\/\/)?(?:www.)?(dailymotion\.com|dai\.ly)\/((video\/([^_]+))|(hub\/([^_]+)|([^\/_]+)))$
GGO
  • 2,678
  • 4
  • 20
  • 42
0

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

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

Debuggex Demo

bgondy
  • 1,198
  • 3
  • 20
  • 32
0

Fix for capturing only the video id

^.*(?:dailymotion.com\/(?:video|hub)|dai.ly)\/([^_]+)[^#]*(#video=([^_&]+))?