删除最后一个反斜杠后的所有内容 [英] Remove everything after last backslash
本文介绍了删除最后一个反斜杠后的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var t = "\some\route\here"
我需要它的\some\route".
I need "\some\route" from it.
谢谢.
推荐答案
你需要 lastIndexOf
和 substr
...
var t = "\\some\\route\\here";
t = t.substr(0, t.lastIndexOf("\\"));
alert(t);
此外,您需要将字符串中的 \
字符加倍,因为它们用于转义特殊字符.
Also, you need to double up \
chars in strings as they are used for escaping special characters.
更新由于这经常被证明对其他人有用,因此这里有一个片段示例...
Update Since this is regularly proving useful for others, here's a snippet example...
// the original string
var t = "\\some\\route\\here";
// remove everything after the last backslash
var afterWith = t.substr(0, t.lastIndexOf("\\") + 1);
// remove everything after & including the last backslash
var afterWithout = t.substr(0, t.lastIndexOf("\\"));
// show the results
console.log("before : " + t);
console.log("after (with \\) : " + afterWith);
console.log("after (without \\) : " + afterWithout);
这篇关于删除最后一个反斜杠后的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文