如何获取网址的最后一段?我有以下脚本,该脚本显示了单击的锚标记的完整网址:
$(".tag_name_goes_here").live('click', function(event)
{
event.preventDefault();
alert($(this).attr("href"));
});
如果网址是
http://mywebsite/folder/file
如何只在警报框中显示网址的“文件”部分?
如何获取网址的最后一段?我有以下脚本,该脚本显示了单击的锚标记的完整网址:
$(".tag_name_goes_here").live('click', function(event)
{
event.preventDefault();
alert($(this).attr("href"));
});
如果网址是
http://mywebsite/folder/file
如何只在警报框中显示网址的“文件”部分?
Answers:
您还可以使用lastIndexOf()函数/
在URL中找到该字符的最后一次出现,然后使用substring()函数从该位置开始返回子字符串:
console.log(this.href.substring(this.href.lastIndexOf('/') + 1));
这样,您就可以避免创建包含所有URL段的数组split()
。
admin/store/tour/-1/
这样,它将是''
字符串吗?
/
末尾包含怎么办?
/
末尾包含:var url = window.location.href.replace(/\/$/, ''); /* remove optional end / */ var lastSeg = url.substr(url.lastIndexOf('/') + 1);
var parts = 'http://mywebsite/folder/file'.split('/');
var lastSegment = parts.pop() || parts.pop(); // handle potential trailing slash
console.log(lastSegment);
http://mywebsite/folder/file/
"" == false
中,空字符串是假的,在这种情况下,它将用空字符串弹出数组的一部分
window.location.pathname.split("/").pop()
/
。console.log(history.location.pathname.split("/").pop())
如果有更好的方法,我想知道!希望此评论可以引导更多的人来回答您。感谢@ Dblock247
.../file?var=value&foo=bar
),则此方法不起作用。此解决方案以更健壮和更现代的方式解决了该问题:stackoverflow.com/a/51795122/1123743
正则表达式的另一种解决方案。
var href = location.href;
console.log(href.match(/([^\/]*)\/*$/)[1]);
如果路径很简单(仅由简单路径元素组成),则其他答案可能会起作用。但是,当它还包含查询参数时,它们就会中断。
更好地使用网址为此对象,以获得更可靠的解决方案。它是对当前URL的解析解释:
输入:
const href = 'https://stackoverflow.com/boo?q=foo&s=bar'
const segments = new URL(href).pathname.split('/');
const last = segments.pop() || segments.pop(); // Handle potential trailing slash
console.log(last);
输出: 'boo'
这适用于所有常见的浏览器。只有我们垂死的IE不支持(并且不会)。对于IE,有一个polyfills可用(如果您很在意的话)。
或者您可以使用正则表达式:
alert(href.replace(/.*\//, ''));
alert(window.location.href.replace(/\/$/, '').replace(/.*//, ''))
我知道,为时已晚,但对于其他人:我强烈建议使用PURL jquery plugin。PURL的动机是,URL也可以用“#”进行分段(例如:angular.js链接),即url看起来像
http://test.com/#/about/us/
要么
http://test.com/#sky=blue&grass=green
借助PURL,您可以轻松决定(细分/细分)您要获取的细分。
对于“经典”最后一段,您可以编写:
var url = $.url('http://test.com/dir/index.html?key=value');
var lastSegment = url.segment().pop(); // index.html
// Store original location in loc like: http://test.com/one/ (ending slash)
var loc = location.href;
// If the last char is a slash trim it, otherwise return the original loc
loc = loc.lastIndexOf('/') == (loc.length -1) ? loc.substr(0,loc.length-1) : loc.substr(0,loc.lastIndexOf('/'));
var targetValue = loc.substr(loc.lastIndexOf('/') + 1);
targetValue = 1
如果您的网址看起来像:
要么
要么
然后loc最终看起来像:http : //test.com/one
现在,由于您需要最后一项,因此请运行下一步以加载您最初想要的值(targetValue)。
var targetValue = loc.substr(loc.lastIndexOf('/') + 1);
search
或,则失败hash
。
window.alert(this.pathname.substr(this.pathname.lastIndexOf('/') + 1));
使用本机pathname
属性,因为它最简单,并且已经由浏览器解析和解析。$(this).attr("href")
可以返回类似的值../..
无法获得正确结果的值。
如果您需要保留search
and hash
(例如foo?bar#baz
from http://quux.com/path/to/foo?bar#baz
),请使用以下命令:
window.alert(this.pathname.substr(this.pathname.lastIndexOf('/') + 1) + this.search + this.hash);
pathname
除去查询参数,但href
不要这样做。
您可以先删除结尾是否有/,然后获取网址的最后一部分
let locationLastPart = window.location.pathname
if (locationLastPart.substring(locationLastPart.length-1) == "/") {
locationLastPart = locationLastPart.substring(0, locationLastPart.length-1);
}
locationLastPart = locationLastPart.substr(locationLastPart.lastIndexOf('/') + 1);
返回最后一个段,而不管后面是否有斜杠:
var val = 'http://mywebsite/folder/file//'.split('/').filter(Boolean).pop();
console.log(val);
path.sep
文字斜杠。请参阅nodejs.org/api/path.html#path_path_sep。这将使您的代码可跨操作系统移植。
更新了raddevus答案:
var loc = window.location.href;
loc = loc.lastIndexOf('/') == loc.length - 1 ? loc.substr(0, loc.length - 1) : loc.substr(0, loc.length + 1);
var targetValue = loc.substr(loc.lastIndexOf('/') + 1);
将url的最后路径打印为字符串:
test.com/path-name = path-name
test.com/path-name/ = path-name
我正在使用正则表达式并拆分:
var last_path = location.href.match(/。/ (。 [\ w])/)[1] .split(“#”)[0] .split(“?”)[0]
最终它将忽略#?&/结束网址,这种情况经常发生。例:
https://cardsrealm.com/profile/cardsRealm- >返回cardsRealm
https://cardsrealm.com/profile/cardsRealm#hello- >返回cardsRealm
https://cardsrealm.com/profile/cardsRealm?hello- >返回cardsRealm
https://cardsrealm.com/profile/cardsRealm/- >返回cardsRealm
我真的不知道正则表达式是否是解决此问题的正确方法,因为它确实会影响代码的效率,但是下面的正则表达式将帮助您获取最后一个段,即使URL,它仍然会为您提供最后一个段后面跟着一个空的/
。我想出的正则表达式是:
[^\/]+[\/]?$