Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript/jquery从url中提取文件名?_Javascript_Jquery_Regex - Fatal编程技术网

如何使用javascript/jquery从url中提取文件名?

如何使用javascript/jquery从url中提取文件名?,javascript,jquery,regex,Javascript,Jquery,Regex,某个变量可能包含相对路径或绝对路径。无论哪种方式,我都需要能够从变量中提取文件名: http://www.somesite.com/dir1/dir2/filename.gif /dir1/dir2/filename.gif 目录结构也是任意的。因此,基本上给定上面的url(任意目录结构),我需要拉取'filename.gif'。提前感谢使用正则表达式,沿着以下几行应该可以做到: [^/]+\.[^/]+ 虽然这并不能涵盖所有可能的情况,但它应该更适合大多数情况。您可以使用以下方法: var

某个变量可能包含相对路径或绝对路径。无论哪种方式,我都需要能够从变量中提取文件名:

http://www.somesite.com/dir1/dir2/filename.gif
/dir1/dir2/filename.gif

目录结构也是任意的。因此,基本上给定上面的url(任意目录结构),我需要拉取'filename.gif'。提前感谢

使用正则表达式,沿着以下几行应该可以做到:

[^/]+\.[^/]+
虽然这并不能涵盖所有可能的情况,但它应该更适合大多数情况。您可以使用以下方法:

var url = window.location.href;
var regex = new RegExp("[^/]+\.[^/]+");
var fileName = regex.exec(url);

希望这有帮助,我会使用正则表达式

[^/]*$
它选择最后一个斜杠之后的所有内容,直到结束。您可以将其展开以单独选择扩展名:

/([^/]*?)(\.[^\./]*)?$
捷径

var filename = window.location.href.substr(window.location.href.lastIndexOf("/")+1);

对于您的示例,子字符串搜索可能是您的最佳选择

但是,如果您的URI实际上足够复杂,您可以尝试Steven Levithan的:

它有两种模式,每种模式都有自己的怪癖,所以一定要记住

如果您不希望查询字符串成为文件名的一部分(您可能不希望),请使用此选项。

var filename=/[^\\\/:*?“\r\n]+$/i.exec(window.location.href)[0];

谢谢sam deng,看起来不错,但你有一个打字错误,我的朋友:

// Extract filename from current page.
var filePath = window.location.pathname;
var fileName = filePath.substr(filePath.lastIndexOf("/") + 1);

如果您的url如下所示:

yourdomain.com/app_dev.php
yourdomain.com/app_dev.php/article/2
您只需拉取文件名(/app_dev.php),就可以链接到您的主页,使用以下命令:

var filename = window.location.pathname.substr(0, window.location.pathname.indexOf("/", 1));
window.location = filename;

在考虑制作数组与使用其他人提到的substr时,可能存在一些我不知道的开销。因此,这可能不是一个很好的答案,而是解决同一问题的不同方法。

我通过以下方法解决了这个问题:

var filename= url.split('/').pop()
var URL = window.location.pathname; // Gets page name
var page = URL.substring(URL.lastIndexOf('/') + 1); 
// then display your filename to console
console.info(page)

这比使用正则表达式快吗?我得到的是
/filename.gif
而不是
filename.gif
。在调用substr之前,索引应该增加1。使用
slice
而不是
substr
:@Gregoire如果/是一个\如何获得文件名?@Pomster var index=yourstring.lastinexof(“\\”)+1;不。这将导致匹配
[“http:”]
。很好,但您是否可以扩展最终删除参数的功能,包括“?" ? Thanks@JamesCazzetta:对于您的需要,这可能有些过分,但这会从URI中获得您所需的一切:谢谢您的帮助,但您应该将此作为对相关答案的注释,而不是发布新答案(将来您可以建议编辑其他人的答案)。这不支持filename.php/arctile/slug-name(它将返回slug name)我建议在您仅检索值时使用代替
window.location.href
。另请参阅可能的重复:我发现这个正则表达式更清晰,但它不是用JavaScript编写的……下面是完整的内容:
url.match(“[^/]*$”)[0]
什么是urlPath?它不是变量或窗口属性。
var filename = /[^\\\/:*?"<>|\r\n]+$/i.exec(window.location.href)[0];
// Extract filename from current page.
var filePath = window.location.pathname;
var fileName = filePath.substr(filePath.lastIndexOf("/") + 1);
var URL = window.location.pathname; // Gets page name
var page = URL.substring(URL.lastIndexOf('/') + 1);  
alert(page);
yourdomain.com/app_dev.php
yourdomain.com/app_dev.php/article/2
var filename = window.location.pathname.substr(0, window.location.pathname.indexOf("/", 1));
window.location = filename;
    var pathnameArray = window.location.pathname.split("/");
    var nameOfFile = pathnameArray[pathnameArray.length -1];
var filename= url.split('/').pop()
var URL = window.location.pathname; // Gets page name
var page = URL.substring(URL.lastIndexOf('/') + 1); 
// then display your filename to console
console.info(page)