If you know the order of the elements in the URL is fixed (I.e. there is nothing before the /customers
segment, like a site/languages prefixes - /uk
), you can just split
the URL and grab the 2nd element in the resulting array. Like so:
var urlParts = url.split('/');
var customerId = urlParts[1]; // Arrays positions start at 0.
Or more elegantly:
var customerId = url.split('/')[1];
If you're not sure about the URL structure, but know that the customer ID is always right after the /customers
segment, you can use regular expression to extract it.
UPDATE: I just realized you were referring to Java, but the above solutions are still valid. Here's a similar question about URL splitting in Java: In java, what's the best way to read a url and split it into its parts?