Get file name from URL using regular expressions or Javascript

I need to get the file name from the url.

Here are the criteria:

He needs to return the empty string "" in the following scripts:

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

And return filename.php in the following scripts:

 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 regex

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

however, it returns somedomain.com at the input of http://somedomain.com . I can’t understand how to change it to ignore the domain if it isn’t at the end.

If it's hard to do with regular expressions, I will also appreciate the JavaScript solution.

Thanx in advance.

+6
javascript url regex filenames
source share
4 answers

Assuming you are writing a script in a browser, you already have a full-featured URL parser so you can use it without having to write an unreliable incomplete regular expression. Use HTMLAnchorElement to read the properties of location -like 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 
+20
source share

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

(ps http://rentzsch.github.com/JSRegexTeststand/ is your friend for this kind of tests)

+3
source share

Use this modified version of Reg ex: (added \ / to an existing one)

 [\w_.-]*?(?=[\/\?\#])|[\w_.-]*$ 
0
source share
 function returnPHPname(x) { var fileName = x.split(/[#\?]/).shift().split('/').pop() return fileName.slice(-3) == 'php'? fileName: "" } 

split(/[#\?]/) split the input into "#" or "?" regex class .
shift() shifts the "leftmost" element from shared input.
split('/') splits this element into each slash and returns an array.
pop() pops the topmost element of the array as the file name.
slice(-3) off the last three characters from the file to check ..
'php'? fileName: "" 'php'? fileName: "" 'php'? fileName: "" 'php'? fileName: "" 'php' returns the file name, otherwise an empty string.

Note that ' \? 'in the regular expression is escaped to be a character instead of the regular expression operator.

0
source share

All Articles