var url = "http://example.com/index.htm"; var filename = url.match(/([^\/]+)(?=\.\w+$)/);
Let's go through the regular expression:
[^\/]+ # one or more character that isn't a slash (?= # open a positive lookahead assertion \. # a literal dot character \w+ # one or more word characters $ # end of string boundary ) # end of the lookahead
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.
var url = "http://example.com/index.htm"; var filename = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));