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["/"]);
5 个解决方案
#1
76
You have the right idea just replace the brackets with parentheses.
您有正确的想法只需用括号替换括号。
var string = "/Roland/index.php";
var result = string.substring(string.lastIndexOf("/") + 1);
Here is an example in jsfiddle and here is an explanation of the .lastIndexOf() method on the Mozilla Developer Network.
这是jsfiddle中的一个示例,这里是对Mozilla Developer Network上的.lastIndexOf()方法的解释。
#2
9
Personally I'd use a regular expression:
就个人而言,我会使用正则表达式:
var result = string.replace(/^.*\/(.*)$/, "$1");
If you're familiar with regular expressions (and you should be if not :-) then it's not as alien-looking as it is when they're unfamiliar.
如果你熟悉正则表达式(如果不是,那么你应该是这样的:-)那么当它们不熟悉时,它并不像外星人一样。
The leading ^
forces this regular expression to "anchor" the match at the start of the string. The \/
matches a single /
character (the \
is to keep the /
from confusing the regular expression parser). Then (.*)$
matches everything else from the /
to the end of the string. The initial .*
will swallow up as much as it can, including /
characters before the last one. The replacement text, "$1"
, is a special form that means "the contents of the first matched group". This regex has a group, formed by the parentheses around the last .*
(in (.*)$
). That's going to be all the stuff after the last /
, so the overall result is that the whole string is replaced by just that stuff. (If the pattern doesn't match because there aren't any /
characters, nothing will happen.)
前导^强制此正则表达式在字符串的开头“锚定”匹配。 \ /匹配单个/字符(\是为了防止混淆正则表达式解析器)。然后(。*)$匹配从/到字符串末尾的所有其他内容。最初的。*将尽可能多地吞噬,包括最后一个之前的/字符。替换文本“$ 1”是一种特殊形式,表示“第一个匹配组的内容”。这个正则表达式有一个组,由最后一个括号括起来。*(in(。*)$)。这将是最后一个/之后的所有内容,因此整体结果是整个字符串被这些东西取代。 (如果模式不匹配,因为没有任何/字符,则不会发生任何事情。)
#3
6
Split the string into an array on /
and .pop()
off the last element. Note, that you will first need to strip off a trailing slash if there is one.
将字符串拆分为最后一个元素上的/和.pop()上的数组。请注意,如果有斜杠,首先需要去掉尾部斜杠。
var locationstring = window.location.pathname;
// replace() the trailing / with nothing, split on the remaining /, and pop off the last one
console.log(locationstring.replace(/\/$/, "").split('/').pop());
If in the case of a URL like /path/stuff/here/
where you have the trailing /
, if that case should return an empty string rather than here
, modify the above to remove the .replace()
from the call chain. I assumed you would want the last component regardless of a trailing slash, but may have incorrectly assumed.
如果在像/ path / stuff / here /这样的URL的情况下你有尾随/,如果那种情况应该返回一个空字符串而不是在这里,修改上面的内容以从调用链中删除.replace()。我假设你想要最后一个组件而不管尾部斜杠,但可能错误地假设了。
console.log(locationstring.split('/').pop());
#4
2
var result = /\/([^\/]*)$/.exec(location)[1];
//"remove-everything-before-the-last-occurrence-of-a-character#10767835"
Note: location
here is the window.location
, not your var location
.
注意:这里的位置是window.location,而不是你的var位置。
#5
0
var string = "/Roland/index.php";
var result = string.substring(0, string.lastIndexOf("/") + 0);
#1
76
You have the right idea just replace the brackets with parentheses.
您有正确的想法只需用括号替换括号。
var string = "/Roland/index.php";
var result = string.substring(string.lastIndexOf("/") + 1);
Here is an example in jsfiddle and here is an explanation of the .lastIndexOf() method on the Mozilla Developer Network.
这是jsfiddle中的一个示例,这里是对Mozilla Developer Network上的.lastIndexOf()方法的解释。
#2
9
Personally I'd use a regular expression:
就个人而言,我会使用正则表达式:
var result = string.replace(/^.*\/(.*)$/, "$1");
If you're familiar with regular expressions (and you should be if not :-) then it's not as alien-looking as it is when they're unfamiliar.
如果你熟悉正则表达式(如果不是,那么你应该是这样的:-)那么当它们不熟悉时,它并不像外星人一样。
The leading ^
forces this regular expression to "anchor" the match at the start of the string. The \/
matches a single /
character (the \
is to keep the /
from confusing the regular expression parser). Then (.*)$
matches everything else from the /
to the end of the string. The initial .*
will swallow up as much as it can, including /
characters before the last one. The replacement text, "$1"
, is a special form that means "the contents of the first matched group". This regex has a group, formed by the parentheses around the last .*
(in (.*)$
). That's going to be all the stuff after the last /
, so the overall result is that the whole string is replaced by just that stuff. (If the pattern doesn't match because there aren't any /
characters, nothing will happen.)
前导^强制此正则表达式在字符串的开头“锚定”匹配。 \ /匹配单个/字符(\是为了防止混淆正则表达式解析器)。然后(。*)$匹配从/到字符串末尾的所有其他内容。最初的。*将尽可能多地吞噬,包括最后一个之前的/字符。替换文本“$ 1”是一种特殊形式,表示“第一个匹配组的内容”。这个正则表达式有一个组,由最后一个括号括起来。*(in(。*)$)。这将是最后一个/之后的所有内容,因此整体结果是整个字符串被这些东西取代。 (如果模式不匹配,因为没有任何/字符,则不会发生任何事情。)
#3
6
Split the string into an array on /
and .pop()
off the last element. Note, that you will first need to strip off a trailing slash if there is one.
将字符串拆分为最后一个元素上的/和.pop()上的数组。请注意,如果有斜杠,首先需要去掉尾部斜杠。
var locationstring = window.location.pathname;
// replace() the trailing / with nothing, split on the remaining /, and pop off the last one
console.log(locationstring.replace(/\/$/, "").split('/').pop());
If in the case of a URL like /path/stuff/here/
where you have the trailing /
, if that case should return an empty string rather than here
, modify the above to remove the .replace()
from the call chain. I assumed you would want the last component regardless of a trailing slash, but may have incorrectly assumed.
如果在像/ path / stuff / here /这样的URL的情况下你有尾随/,如果那种情况应该返回一个空字符串而不是在这里,修改上面的内容以从调用链中删除.replace()。我假设你想要最后一个组件而不管尾部斜杠,但可能错误地假设了。
console.log(locationstring.split('/').pop());
#4
2
var result = /\/([^\/]*)$/.exec(location)[1];
//"remove-everything-before-the-last-occurrence-of-a-character#10767835"
Note: location
here is the window.location
, not your var location
.
注意:这里的位置是window.location,而不是你的var位置。
#5
0
var string = "/Roland/index.php";
var result = string.substring(0, string.lastIndexOf("/") + 0);