【发布时间】:2019-11-10 12:08:27
【问题描述】:
目前使用我的代码:
window.location.pathname.split('/')[3]
我可以从以下方面得到“比较”:
http://localhost/study/78/comparative
但可能会在 URI 中添加一些子目录。
如何获取以/ 分隔的URI 上的最后一个子字符串(最右边)?
【问题讨论】:
-
window.location.pathname.split('/').pop() -
x.substr(x.lastIndexOf("/") + 1);是一种方式 -
我更喜欢 Alex 的更好、更少的说明。虽然我个人更喜欢
String.prototype.slice()而不是String.prototype.substr()。 -
window.location.pathname.split("/").slice(-1)[0];是一种方式 -
另一个(但效率稍低)
window.location.pathname.split('/').reverse()[0]
标签: javascript