0

I am trying to extract the string which is like

var str = "[\"/home/dev/servers\", \"e334ffssfds245fsdff2f\"]"

Desired ouput

a = "/home/dev/servers"
b = "e334ffssfds245fsdff2f"
6
  • 1
    Invalid str. " should be after ] Commented Apr 9, 2019 at 13:16
  • Is it allways like that ? Commented Apr 9, 2019 at 13:17
  • 1
    So why is it not just an array to start? Issue here is the string is flawed from the start, all of those slashed need to be doubled up [\"\\home\\dev\\servers\",... Commented Apr 9, 2019 at 13:18
  • So will the format always be the same? and have you tried str.split(',')? Commented Apr 9, 2019 at 13:25
  • @Moad Ennagi it does not in my System. Are you sure? Commented Apr 9, 2019 at 13:30

3 Answers 3

2

Here you are:

const str = "[\"/home/dev/servers\", \"e334ffssfds245fsdff2f\"]";

const object = JSON.parse(str);

const a = object[0];
const b = object[1];

console.log(a);
console.log(b);

Sign up to request clarification or add additional context in comments.

1 Comment

I am getting Argument of type '_Blob' is not assignable to parameter of type 'string'. Type 'Buffer' is not assignable to type 'string'.
2

The following will work fine for you.

var str = "[\"/home/dev/servers\", \"e334ffssfds245fsdff2f\"]";

var foo = JSON.parse(str); //Parse the JSON into an object.

var a = foo[0];
var b = foo[1];

5 Comments

I just changed the code, could you please check it again
This will not output OP expected results.
Updated answer to reflect OP's changes.
I am getting **Argument of type '_Blob' is not assignable to parameter of type 'string'. Type 'Buffer' is not assignable to type 'string'. **
@BhargavTeja that error is unrelated to this question, or the code that is creating that error has not been posted in your question.
0

Using JSON.parse()

let [a, b] = JSON.parse("[\"/home/dev/servers\", \"e334ffssfds245fsdff2f\"]")

console.log(a)
console.log(b)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.