I want to parse Dailymotion video url to get video id in javascript, like below:
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?
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?
Share Improve this question asked Sep 12, 2012 at 11:42 relowerrelower 1,3131 gold badge10 silver badges20 bronze badges5 Answers
Reset to default 14function 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
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"));
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\/([^_]+)|([^\/_]+)))$
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
Fix for capturing only the video id
^.*(?:dailymotion.com\/(?:video|hub)|dai.ly)\/([^_]+)[^#]*(#video=([^_&]+))?