I think for any url related task you have two good tools as
- The URL API
- The URLPattern API
With the URL
constructor you may do like;
var url = new URL("https://any-api/supplier/tariffSelectionG_E5449168?t=randomChars"), tariffCode = url.pathname.slice(url.pathname.lastIndexOf("/")+1);
URLPattern
API, on the other hand is more powerful and convenient for this job despite being a little comprehensive. Keep in mind that it's a recent API and available in Chrome 95+. Server side, it is available in Deno (which i like very much) and possibly in recent Node versions too.
In this particular case we can generate an URL Pattern which takes the path up until a variable that we declare. This variable represents the url portion of our interest. Lets name it tariff
. Accordingly
var pat = new URLPattern({pathname: "/supplier/:tariff"});
Thats it.
Once you receive an url first you can test it's validity against the pattern like;
pat.test("https://any-api/supplier/tariffSelectionG_E5449168?t=randomChars"); // <- true
If true
then you can extract your tariff code like
var tariffCode = pat.exec("https://any-api/supplier/tariffSelectionG_E5449168?t=randomChars") .pathname.groups.tariff; // <- "tariffSelectionG_E5449168"
re = /https.*?supplier\/(?<param>.*)\?/g; re.exec(supplyUrl).groups["param"]
to retrieve the same result more compact.\$\endgroup\$