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 studied regular expression for a while.
This is an updated version that returns an array of any dailymotion identifier found in the 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; }
check here http://jsfiddle.net/18upkjaa/embedded/result/ by entering a text box
Roman source share