I have a string:
http://a.long.url/can/be/here/jquery.min.js?207
I need to extract the base URL http://a.long.url/can/be/here/ using Javascript. So it should be split on the first / from the right.
url.substring(0, url.lastIndexOf("/") + 1)
Remove the + 1 if you don't want the / in the end.
/, e.g. if it's simply www.mydomain.com.var url = "http://my.com:8080/path/bob.js?query"
var col1 = url.indexOf("?");
if (col1 > 0) {
var col = url.substring(0,col1).lastIndexOf("/");
} else {
col = url.lastIndexOf("/");
}
var extracted = url.substring(0, col);
Without checking for errors. (For example, what if the url string has no slashes at all. It's not a valid URL but it could be passed to this code.) Note also that it doesn't work if the question mark is the first character.
str.split("/").slice(0, -1).join("/")?