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?

5
  • 4
    Backslash should be escaped with another backslash in the string.
    – VisioN
    Jun 25, 2014 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? Jun 25, 2014 at 11:20
  • @ Vision :the string cannot be changed.. Jun 25, 2014 at 11:22
  • @ T.J Crowder: str.split() method is not working with the backslash..but when i remove them it works fine. Jun 25, 2014 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). Jun 25, 2014 at 11:32

5 Answers 5

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

6
  • 4
    You wanted to write var parts = str.split("\\");?
    – VisioN
    Jun 25, 2014 at 11:29
  • @VisioN: Thank you for catching that! Jun 25, 2014 at 11:32
  • @T. J Crowder: You have added one more backslash in the string right? Jun 25, 2014 at 11:32
  • But there will be only one backslash in the original string Jun 25, 2014 at 11:33
  • @user3774907: In a string literal, if you want an actual backslash, you write two of them. That's because in a string literal, a backslash is an "escape" character. What it does depends on the next character. \" is a " within the string, a double backslash produces a single backslash in the string, \n is a newline, etc. Jun 25, 2014 at 11:33
4

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

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

1
  • 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"] Jul 8, 2021 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.

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(/[\$]/)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.