remove last directory in URL

Tags: , , , ,

I am trying to remove the last directory part of an URL. My URL looks like this:


When clicking on a button, I want to change this to

https://my_ip_address:port/site.php?path=/path/to/my. (Remove the last part).

I already tried window.location.replace(//[A-Za-z0-9%]+$/, ""), which results in


What Regex should I use to do this?


Explanation: Explode by “/”, remove the last element with pop, join again with “/”.

function RemoveLastDirectoryPartOf(the_url)
    var the_arr = the_url.split('/');
    return( the_arr.join('/') );

see fiddle

Source: stackoverflow