I am trying to create a Javascript Regex that captures the filename without the file extension. I have read the other posts here and 'goto this page: http://gunblad3.blogspot.com/2008/05/uri-url-parsing.html' seems to be the default answer. This doesn't seem to do the job for me. So here is how I'm trying to get the regex to work:
- Find the last forward slash '/' in the subject string.
- Capture everything between that slash and the next period.
The closest I could get was : /([^/]).\w$ Which on the string 'http://example.com/index.htm' exec() would capture /index.htm and index.
I need this to only capture index.
Let's go through the regular expression:
This expression will collect all characters that aren't a slash that are immediately followed (thanks to the lookahead) by an extension and the end of the string -- or, in other words, everything after the last slash and until the extension.
Alternately, you can do this without regular expressions altogether, by finding the position of the last
/
and the last.
usinglastIndexOf
and getting asubstring
between those points:You can try this regex :
I did not find any of the answers to be near robust enough. Here is my solution.
To fit with the original question, the default behavior is to exclude the extension, but that can easily be reversed.
tested and works, even for pages without file extension.
([\w\d_-]*)
get a string containing letters, digits, underscores or hyphens.\.?
perhaps the string is followed by a period.[^\\\/]*$
but certainly not followed by a slash or backslash till the very end./i
oh yeh, ignore case.