Skip to content Skip to sidebar Skip to footer

Regexp Get Last Part Of Url Without Parameters If They Exists

I looking for regular expression to use in my javascript code, which give me last part of url without parameters if they exists - here is example - with and without parameters: htt

Solution 1:

Here is this regexp

.*\/([^?]+)

and JS code:

let lastUrlPart = /.*\/([^?]+)/.exec(url)[1];

letlastUrlPart = url => /.*\/([^?]+)/.exec(url)[1];



// TESTlet t1 = "https://scontent-fra3-1.xx.fbcdn.net/v/t1.0-9/14238253_132683573850463_7287992614234853254_n.jpg?oh=fdbf6800f33876a86ed17835cfce8e3b&oe=599548AC"let t2 = "https://scontent-fra3-1.xx.fbcdn.net/v/t1.0-9/14238253_132683573850463_7287992614234853254_n.jpg"console.log(lastUrlPart(t1));
console.log(lastUrlPart(t2));

May be there are better alternatives?

Solution 2:

You could always try doing it without regex. Split the URL by "/" and then parse out the last part of the URL.

var urlPart = url.split("/");
var img = urlPart[urlPart.length-1].split("?")[0];

That should get everything after the last "/" and before the first "?".

Post a Comment for "Regexp Get Last Part Of Url Without Parameters If They Exists"