最后一次出现,我有以下字符串:“\ IonSubsystem1 \ TRAN-88311”删除字符串
我要拉断的最后一个字:TRAN-88311是一个ID,在一个通用的方法。
我该怎么办呢?
我曾想过,也许发现的“\”信中最后一次出现,但在我看来,我不能运行JS顺序如下:
var a = "\\IonSubsystem1\TRAN-88311";
a.lastIndexOf('\') ==> error in here.
任何其他解决办法是值得欢迎的,谢谢提前
最后一次出现,我有以下字符串:“\ IonSubsystem1 \ TRAN-88311”删除字符串
我要拉断的最后一个字:TRAN-88311是一个ID,在一个通用的方法。
我该怎么办呢?
我曾想过,也许发现的“\”信中最后一次出现,但在我看来,我不能运行JS顺序如下:
var a = "\\IonSubsystem1\TRAN-88311";
a.lastIndexOf('\') ==> error in here.
任何其他解决办法是值得欢迎的,谢谢提前
这很容易。
var a = "\\IonSubsystem1\\TRAN-88311";
a = a.split("\\");
console.log(a[a.length-1]);
在你的字符串表示时,\T
被转换为制表符。
如果split \ in string'“\\”' – Mahi
@Mahi你怎么办?那是不可能的。 –
这应做到:
var a = "\\IonSubsystem1\\TRAN-88311";var b = a.split('\\');console.log(b[b.length-1]);
当然,你必须转义反斜线,否则顶点将被替代逃脱(引发错误)
错误
x.indexOf('\'); //The first apex opens the string, the slash will prevent the second apex to close it
正确
x.indexOf('\\'); //The first apex open the string, the first backslash will escape the second one, and the second apex will correctly close the string
同时检查答案中的字符串的颜色,您可以很容易地看到错误的引起注释显示为文本(因为字符串从未关闭),而在正确的一个中,一切按预期行事 –
在这里使用Regex捕获组是一个解决方案。
var a = '\\IonSubsystem1\\TRAN-88311';
var id = a.replace(/(\\.*\\)(.*)/g, '$2');
alert(id);
你是如何想的子串的边界以去除定义? – Richard
让评论反斜杠非常困难。 – Pointy