8

I have a string containing two backslashes in it:

str = "active - error - oakp-ms-001 Volume Usage-E:\ PercentUsed E:\"

I want to pick up only "oakp-ms-001" from the above string, but as the string contains backslash in it I am not able to split the string.

Please let me know if there is any solution for this?

Biffen
  • 6,249
  • 6
  • 28
  • 36
user3774907
  • 99
  • 1
  • 1
  • 2
  • 4
    Backslash should be escaped with another backslash in the string. – VisioN Jun 25 '14 at 11:19
  • *"...but as the string contains backslash in it i am not able to split the string."* Why not? What does the backslash have to do with the content you've said you want to grab? – T.J. Crowder Jun 25 '14 at 11:20
  • @ Vision :the string cannot be changed.. – user3774907 Jun 25 '14 at 11:22
  • @ T.J Crowder: str.split() method is not working with the backslash..but when i remove them it works fine. – user3774907 Jun 25 '14 at 11:24
  • 1
    @user3774907: The quoted code doesn't produce anything, as it has no ending `"` on the string (the `\"` at the end is a `"` character *in* the string -- or would be, if the string ended somewhere). – T.J. Crowder Jun 25 '14 at 11:32

5 Answers5

11

First, I'll note that the code you've quoted has a syntax error:

str = "active - error - oakp-ms-001 Volume Usage-E:\ PercentUsed E:\"

There's no ending " on that string, becaue the \" at the end is an escaped ", not a backslash followed by an ending quote.

If there were an ending quote on the string, it would have no backslashes in it (the \ with the space after it is an invalid escape that ends up just being a space).

So let's assume something valid rather than a syntax error:

str = "active - error - oakp-ms-001 Volume Usage-E:\\ PercentUsed E:\\";

That has backslashes in it.

What you need to do doesn't really involve "splitting" at all but if you want to split on something containing a backslash:

var parts = str.split("\\"); // Splits on a single backslash

But I'm not seeing how splitting helps with what you've said you want.

You have to identify what parts of the string near what you want are consistent, and then create something (probably a regular expression with a capture group) that can find the text that varies relative to the text that doesn't.

For instance:

var str = "active - error - oakp-ms-001 Volume Usage-E:\\ PercentUsed E:\\";
var match = str.match(/error - (.*?) ?Volume/);
if (match) {
    console.log(match[1]); // oakp-ms-001
}

There I've assumed the "error - " part and the "Volume" part (possibly with a space in front of it) are consistent, and that you want the text between them.

Live Example

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
4

JSON.stringify(fileName).split(“\”);

It’s should be double backslash inside the splitenter image description here

Habeeb
  • 322
  • 1
  • 8
  • 1
    Crazy it doesn't work for me. It works but returns it still with the "\" log.slice(log.indexOf("level"), log.indexOf('\",')).split("\\") ["level\":\"Info"] – Lorenzo Covarrubias Jul 08 '21 at 19:38
1

This is a non-terminating string to begin with (you're escaping the closing quotation mark), so I'm going to assume your string looks more like this:

str = "active - error - oakp-ms-001 Volume Usage-E:\\ PercentUsed E:\\";

If you want to split the string by backslashes and spaces alike, the first step is to split by backslashes, done like this:

step2 = str.split("\\");

Note that you have to escape the backslash here.

The second thing to do is to now split this string by spaces, but because it's an array you have to use a loop to do this:

var step3 = [];
for(var i = 0; i < step2.length; i++){
    step3 += step2[i].split(" ");
}

And then you can simply split step3 by "," characters and find the phrase before "Volume". This probably isn't the best answer, but it gets you the data you want.

theStandard
  • 212
  • 2
  • 9
0

escape your backslash! \ becomes \\ so in fact you assign like ths:

str = "active - error - oakp-ms-001 Volume Usage-E:\\ PercentUsed E:\\"

-3

This is a solution for this question str.split(/[\$]/)