0

I'm wanting to remove any filenames from a url path (which doesn't contain the domain) in javascript.

The path would look like this:

/something/myfile.html?d=var

or

/something/myfile.html

and this is the desired output:

/something/

Sometimes urls get passed in like this:

/something/else

I don't want 'else' to be stripped out. In other words a regex that simply strips everything after the last slash would not work in my case.

EDIT: To clarify, I consider a filename anything with a . after a forward slash as seen above.

4

1 回答 1

2

Remove that which matches the following regex pattern:

[^/?]*\.[^/?]*(\?.*)?$

or really

[^/?#]*\.[^/?#]*(\?.*)?(\#.*)?$

Don't forget to use decodeURIComponent on what's left.

于 2012-06-04T16:51:08.717 回答