Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 865
miki725 Avatar asked Dec 28 '10 21:12

miki725


2 Answers

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
like image 140
bobince Avatar answered Oct 21 '22 10:10

bobince


This will put the filename in $1: [^:]+://[^/]+/?([^?#]*)

(p.s. http://rentzsch.github.com/JSRegexTeststand/ is your friend for this sort of test)

like image 39
kevingessner Avatar answered Oct 21 '22 10:10

kevingessner