字符串变量中用逗号分隔的字符串值

时间:2019-04-09 13:15:32

标签: javascript

我正在尝试提取像这样的字符串

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

期望的输出

a = "/home/dev/servers"
b = "e334ffssfds245fsdff2f"

3 个答案:

答案 0 :(得分:2)

以下内容适合您。

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];

答案 1 :(得分:2)

您在这里:

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);

答案 2 :(得分:0)

使用JSON.parse()

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

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