How to parse Dailymotion Video Url in javascript

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

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

video id: "x44lvd"

I think I need a regex string to get the video id for all URL combinations for dailymotion.

I found the regex of the url for the links on YouTube, its working well as 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 someone please give me some tips on dailymotion?

+4
source share
3 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 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

+13
source

Below is a small update to handle the 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")); 
+3
source

Other answers do not match all dailymotion urls

(e.g. http://dai.ly/x2no31b )

Use this regex:

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

Source: https://habr.com/ru/post/1433781/


All Articles