使用正则表达式或 Javascript 从 URL 获取文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4549654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:05:08  来源:igfitidea点击:

Get filename from URL using Regular Expressions or Javascript

javascriptregexurlfilenames

提问by miki725

I need to get the filename from the URL address.

我需要从 URL 地址获取文件名。

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:

并在以下场景中返回 filename.php:

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

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

however it returns somedomain.comon 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.

但是它返回somedomain.cominput http://somedomain.com。我不知道如何修改它以/在它末尾没有时忽略域。

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

如果使用正则表达式很难,我也会很感激 JavaScript 解决方案。

Thanx in advance.

提前谢谢。

回答by bobince

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, hashetc.:

假设您在浏览器中编写脚本,已经有一个功能齐全的 URL 解析器供您利用,而不必编写不可靠的不完整正则表达式。使用一个HTMLAnchorElement读取location式的性质hostpathnamesearchhash等:

var a= document.createElement('a');
a.href= 'http://somedomain.com/dirname/filename.php?query';
var filename= a.pathname.split('/').pop(); // filename.php

回答by kevingessner

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

这会将文件名放入$1[^:]+://[^/]+/?([^?#]*)

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

(ps http://rentzsch.github.com/JSRegexTeststand/是这种测试的朋友)

回答by Chandu

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

使用这个经过调整的 Reg ex 版本:(将 \/ 添加到现有的)

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

回答by Chandu

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

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

split(/[#\?]/)在“#”或“?”上拆分输入 通过正则表达式字符类
shift()转移出从分裂输入“最左边的”元件。在每个斜杠上
split('/')拆分此元素并返回一个数组。
pop()弹出数组的“最顶层”元素作为文件名。从文件名中
slice(-3)下最后三个字符以进行检查..
'php'? fileName: ""'php' 返回文件名,否则为空字符串。

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

请注意,正则表达式中的 ' \?'被转义为字符而不是正则表达式运算符。