开发者

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 expressio开发者_JAVA百科n

[\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.


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)


Use this tweaked version of the Reg ex:(added \/ to the existing one)

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


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

split(/[#\?]/) split input on '#' or '?' by a regex character class.
shift() shift out the "leftmost" element from splitted input.
split('/') split this element on each slash and return an array.
pop() pop the "topmost" element of array as an filename.
slice(-3) slice off three last characters from filename to check..
'php'? fileName: "" 'php' returns filename otherwise empty string.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜