Brk
Brk

Reputation: 1297

Remove the last occurrence of string

I have the following string: "\IonSubsystem1\TRAN-88311"

I need to pull off the last word:TRAN-88311 which is an id, in a generic way.

How can I do it?

I have thought maybe found the last occurrence of '\' letter, but it is appear to me that I can't run the following order in JS :

var a = "\\IonSubsystem1\TRAN-88311";
a.lastIndexOf('\') ==> error in here.

Any other solution is welcome , thanks in advance

Upvotes: 1

Views: 119

Answers (4)

Rajshekar Reddy
Rajshekar Reddy

Reputation: 18987

Using Regex with capturing groups here is a solution.

var a = '\\IonSubsystem1\\TRAN-88311';
var id = a.replace(/(\\.*\\)(.*)/g, '$2');

alert(id);

Upvotes: 1

Luca De Nardi
Luca De Nardi

Reputation: 2321

Of course you have to escape that backslash otherwise the apex will be escaped instead (throwing the error)

Wrong

x.indexOf('\'); //The first apex opens the string, the slash will prevent the second apex to close it

Correct

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

Upvotes: 0

raphael75
raphael75

Reputation: 3228

This should do it:

var a = "\\IonSubsystem1\\TRAN-88311";var b = a.split('\\');console.log(b[b.length-1]);

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167162

This is very easy.

var a = "\\IonSubsystem1\\TRAN-88311";
a = a.split("\\");
console.log(a[a.length-1]);

In your string representation, the \T is converted as a tab character.

Upvotes: 4

Related Questions