I\'m trying to add a \"back to dir\" button at the top of a web page, which would redirect the user to the same URL, but with no filename in it.
For example, clickin
The following gets directories, including the case where the last character is a slash.
document.URL.replace(/\/[^/]+\/?$/, '');
This effectively states the following (assuming they can be found in this order)
\/
: Find a "/"[^/]+
: Find one or more characters after it that are not "/"\/?
: Find an optional "/" after those character(s)$
: Find the end of the stringThen remove these by ''
, so we're left with the directory only.
Again, this assumes there is a slash present marking a directory and that this is not just a URL where the only slashes are within http://
.