I was looking at this post but twitch has recently changed its url for clip into a different format.
The new format is:
https://www.twitch.tv/loserfruit/clip/HungryBillowingSandpiperTebowing
And the ID for the clip is HungryBillowingSandpiperTebowing
How could I extract the ID using regex?
My best attempt is by using replacing method, will such method be able to cover all the grounds?
var Y = "/clip/";
var X = twitchurl;
var Z = X.replace(new RegExp(".*" + Y), "");
Any help would be greatly appreciated.
You attempt to match any zero or more chars other than line break chars, as many as possible, before your Y
(/clip/
string).
Instead, you should match any one or more chars other than /
as many as possible after the Y
part, using [^/]+
.
You may utilize either a lookbehind based solution or a capturing group one:
const Y = "/clip/";
const twitchurl = "https://www.twitch.tv/loserfruit/clip/HungryBillowingSandpiperTebowing";
// Using a capturing group
const result = twitchurl.match(new RegExp(`${Y}([^/]+)`))?.[1];
console.log(result);
// Using a lookbehind
const result2 = twitchurl.match(new RegExp(`(?<=${Y})[^/]+`))?.[0];
console.log(result2);