如何在 JavaScript 中提取当前文档路径的 URL 的文件名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6543242/
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-08-23 22:13:28  来源:igfitidea点击:

How to extract the filename of the URL of the current document path in JavaScript?

javascriptjqueryregex

提问by Kei Izumi

I'm trying to extract the current file name in Javascript without any parameters.

我试图在没有任何参数的情况下在 Javascript 中提取当前文件名。

$(location).attr('href').match(/([a-zA-Z\-\_0-9]+\.\w+)$/);
var current_path = RegExp.;
if ((current_path == 'index.html') || ...) {
  // something here
}

But it doesn't work at all when you access like http://example.com/index.html?lang=ja. Sure before the file name will be changed at random.

但是当您像http://example.com/index.html?lang=ja这样访问时,它根本不起作用。肯定之前的文件名会随机改变。

Any idea?

任何的想法?

回答by user113716

If you're looking for the last item in the path, try this:

如果您要查找路径中的最后一项,请尝试以下操作:

var current_path = window.location.pathname.split('/').pop();

This:

这个:

window.location.pathname

will give you something like:

会给你类似的东西:

"/questions/6543242/how-to-extract-the-filename-of-url-in-javascript"

Then the .split()will split the string into an Array, and .pop()will give you the last item in the Array.

然后.split()将字符串拆分为一个数组,并.pop()为您提供数组中的最后一项。

回答by kennebec

function filename(path){
    path = path.substring(path.lastIndexOf("/")+ 1);
    return (path.match(/[^.]+(\.[^?#]+)?/) || [])[0];
}

console.log(filename('http://example.com/index.html?lang=ja'));

// returned value: 'index.html'

回答by ridgerunner

The filename of a URL is everything following the last "/"up to one of the following: 1.) a "?"(beginning of URL query), or 2.) a "#"(beginning of URL fragment), or 3.) the end of the string (if there is no query or fragment).

URL 的文件名是从最后"/"一个到以下之一的所有内容:1.) a "?"URL 查询的开头),或 2.) a "#"URL 片段的开头),或 3.) 字符串的结尾 (如果没有查询或片段)。

This tested regex does the trick:

这个经过测试的正则表达式可以解决问题:

.match(/[^\/?#]+(?=$|[?#])/);

.match(/[^\/?#]+(?=$|[?#])/);

回答by nekman

There is a URL.jslibrary that makes it very easy to work with URLs. I recommend it!

有一个URL.js库可以很容易地处理 URL。我推荐它!

Example

例子

var uri = new URI('http://example.org/foo/hello.html?foo=bar');
uri.filename(); // => 'hello.html'

回答by Nourdine Alouane

You can do something more simple:

你可以做一些更简单的事情:

var url = "http://google.com/img.png?arg=value#div5"
var filename = url.split('/').pop().split('#')[0].split('?')[0];

Result:

结果:

filename => "img.png"

回答by James Khoury

your regex isn't correct. Instead try to be more specific:

你的正则表达式不正确。相反,请尝试更具体:

.match(/([a-zA-Z\-\_0-9]+\.[a-zA-Z]{2,4})[\?$]/);

says:

说:

find any number of alphanumeric or hypens[a-zA-Z\-\_0-9]+before a fullstop that has between 2 and 4 alphabetic characters [a-zA-Z]{2,4}that combefore either the end (\$) or a question mark (\?)

在句号 ( ) 或问号 ( )[a-zA-Z\-\_0-9]+之前有 2 到 4 个字母字符的句号之前查找任意数量的字母数字或连字符[a-zA-Z]{2,4}\$\?

tested on:

测试:

("http://www.example.com/index.html?lang=ja").match(/([a-zA-Z\-\_0-9]+\.[a-zA-Z]{2,4})[\?$]/);
var current_path = RegExp.;
alert(current_path);

回答by thecodeparadox

try this:

尝试这个:

window.location.pathname.substring(1)