我正在尝试对字符串执行以下操作:
- 查找字符的最后出现
"/"; - 删除该角色之前的所有内容;
- 返回字符串的剩余部分;
更明确地说,假设我有以下字符串:
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)。