0

Please help to build regex for d.tube to extract channel_id and video_id from url

Following urls possible

https://d.tube/v/channel_id/video_id
https://d.tube/#!/v/channel_id/video_id
https://emb.d.tube/#!/channel_id/video_id
TELA
  • 41
  • 1
  • 7
  • The reason your question doesn't really elucidate what you want is because the examples / samples that are listed all include the **exact text-phrase** `channel_id` and the phrase `video_id` I am not able to deduce what you are looking to do. One version would be to **extract** the channel and video id's but another version would be to **verify** that the phrases (`channel_id` and `video_id`) are always present. What are you looking to get from your reg-ex? –  Oct 18 '18 at 16:04
  • @RalphTorello, Sorry! I actually need to extract channel_id and video_id from url – TELA Oct 18 '18 at 16:24

2 Answers2

1

This should capture them two groups: https:\/\/(?:emb)?\.?d.tube\/v?(?:#!)?\/?v?\/?(\w+)\/(\w+)

It firstly matches https then the two forward slashes, which require the \ escape character.

Optionally, it gets emb, then optionally a . then d.tube, optionally /v, optionally #!, optionally another /, optionally another v and / then two capture groups which take alpha numeric characters, split by a /.

Nathan Liu
  • 284
  • 5
  • 13
1

I have posted a comment to explain my point, but here are two (possible!) different versions of what you would want:

https:\/\/(?:\w+\.)?d.tube\/.*?\/([^\/]*)\/([^\/]*)\n

And then this would extract the channel_id' and thevideo_id` from the URL: NOTE: This answer is partially copied from this stack-overflow answer: How do you access the matched groups in a JavaScript regular expression?

var myString = myURL;
var myRegexp = /https:\/\/(?:\w+\.)?d.tube\/.*?\/([^\/]*)\/([^\/]*)\n/g;
var match = myRegexp.exec(myString);
console.log("Channel ID: " + match[0] + "\n" + "Video ID:" + match[1] + "\n");

Here is a screen-capture generated by http://regexr.com (pretty useful utiliity) enter image description here

While this solution here would verify that you had valid "d.tube" URL's

https:\/\/(?:\w+\.)?d.tube\/.*?\/channel_id/video_id\n
var myString = myURL;
var myRegexp = /https:\/\/(?:\w+\.)?d.tube\/.*?\/channel_id/video_id\n/;
var match = myRegexp.exec(myString);
var isMatch = match != null;