Javascript 删除最后一次出现字符之前的所有内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10767815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:50:49  来源:igfitidea点击:

remove everything before the last occurrence of a character

javascript

提问by Roland

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["/"]);

回答by Yevgeny Simkin

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 jsfiddleand here is an explanation of the .lastIndexOf() method on the Mozilla Developer Network.

这是jsfiddle 中的一个示例,这里是Mozilla Developer Network上 .lastIndexOf() 方法的解释。

回答by Michael Berkowski

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.

如果在 URL 的情况下/path/stuff/here/,你有尾随/,如果这种情况应该返回一个空字符串而不是here,请修改上面的内容以.replace()从调用链中删除。我假设无论尾部斜杠如何,您都需要最后一个组件,但可能错误地假设。

console.log(locationstring.split('/').pop());

回答by Pointy

Personally I'd use a regular expression:

我个人会使用正则表达式:

var result = string.replace(/^.*\/(.*)$/, "");

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 (.*)$)周围的括号组成。这将是最后一次之后的所有东西/,所以总体结果是整个字符串都被那个东西替换了。(如果模式不匹配,因为没有任何/字符,什么都不会发生。)

回答by Esailija

    var result = /\/([^\/]*)$/.exec(location)[1];

//"remove-everything-before-the-last-occurrence-of-a-character#10767835"

Note: locationhere is the window.location, not your var location.

注意:location这里是window.location,而不是您的var location.

回答by Ashish Gupta

var string = "/Roland/index.php";
var result = string.substring(0, string.lastIndexOf("/") + 0);