Get filename from URL using Regular Expressions or Javascript
Assuming you are writing script in a browser, there is already a full-featured URL parser for you to take advantage of, without having to write unreliable incomplete regexen. Use an HTMLAnchorElement to read the location
-like properties host
, pathname
, search
, hash
etc.:
var a= document.createElement('a');
a.href= 'http://somedomain.com/dirname/filename.php?query';
var filename= a.pathname.split('/').pop(); // filename.php
This will put the filename in $1
: [^:]+://[^/]+/?([^?#]*)
(p.s. http://rentzsch.github.com/JSRegexTeststand/ is your friend for this sort of test)