在最后一次出现角色之前删除所有内容 [英] remove everything before the last occurrence of a character
本文介绍了在最后一次出现角色之前删除所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试对字符串执行以下操作:
I'm trying to perform the following action on a string :
- 查找字符的最后一次出现
/
; - 删除该字符前的所有内容;
- 返回字符串的剩余部分;
- find the last occurrence of the character
"/"
; - remove everything before that character;
- return the remains of the string;
为了更明确,假设我有以下字符串:
To be more explicit, let's say I have the following string :
var string = "/Roland/index.php"; // Which is a result of window.location.pathname
现在我需要提取它是除了实际页面之外的所有内容,如下所示:
Now what I need to extract out of it is everything but the actual page, something like this :
var result = "index.php" // Which is what I need to be returned
当然,这只是一个例子,因为很明显我会有不同的页面,但是同样的原则适用。
Of course, that is just an example, because obviously I will have different pages, but the same principles apply.
我想知道是否有人可以帮我解决问题。我尝试了下一步但没有成功:
I was wondering if someone could help me out with a solution for it. I tried the next actions but with no success :
var location = window.location.pathname;
var result = location.substring(location.lastIndexOf["/"]);
推荐答案
你有正确的想法只需用括号替换括号。
You have the right idea just replace the brackets with parentheses.
var string = "/Roland/index.php";
var result = string.substring(string.lastIndexOf("/") + 1);
以下是 jsfiddle ,这里是对 Mozilla开发者网络。
这篇关于在最后一次出现角色之前删除所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文