Search code examples
javascriptregexurlfilenames

Get filename from URL using Regular Expressions or Javascript


I need to get the filename from the URL address.

Here is the criteria:

It need to return empty string "" in following scenarios:

http://somedomain.com
http://www.somedomain.com
http://somedomain.com/
http://www.somedomain.com/

And return filename.php in the following scenarios:

http://somedomain.com/filename.php?query
http://www.somedomain.com/filename.php?query
http://somedomain.com/filename.php#query
http://www.somedomain.com/filename.php#query

I found this regular expression

[\w_.-]*?(?=[\?\#])|[\w_.-]*$ from here

however it returns somedomain.com on input http://somedomain.com. I can't figure out how to modify it to ignore the domain when there is no / at the end of it.

If it is difficult to do with regular expressions, I will appreciate a JavaScript solution as well.

Thanx in advance.


Solution

  • 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