【发布时间】:2012-05-26 16:07:19
【问题描述】:
我正在尝试对字符串执行以下操作:
- 找到字符
"/"的最后一次出现; - 删除该字符之前的所有内容;
- 返回字符串的剩余部分;
更明确地说,假设我有以下字符串:
var string = "/Roland/index.php"; // Which is a result of window.location.pathname
现在我需要从中提取除实际页面之外的所有内容,如下所示:
var result = "index.php" // Which is what I need to be returned
当然,这只是一个例子,因为显然我会有不同的页面,但同样的原则适用。
我想知道是否有人可以帮助我解决这个问题。我尝试了接下来的操作,但没有成功:
var location = window.location.pathname;
var result = location.substring(location.lastIndexOf["/"]);
【问题讨论】:
-
你很亲密。
lastIndexOf是一个函数,如果location是一个全局变量,则不能覆盖它。所以它应该是:loc.substring(loc.lastIndexOf("/"))并且因为您不想包含斜线本身:loc.substring(loc.lastIndexOf("/") + 1)。
标签: javascript