Reputation:
I'm trying to convert a string to an array in javascript?
var strng = "[a,b]"
Expected output:
var arry = ["a","b"]
Upvotes: 1
Views: 64
Reputation: 588
Work for all string like "[a,b]"
or "[a,b,c,d]"
var strng = "[a,b]";
console.log(strng.replace(/\[|]/g, "").split(","));
Upvotes: 1
Reputation: 28414
Try this:
var strng = "[a,b]";
let arr = strng.slice(1,-1).split(",");
console.log(arr);
Upvotes: 0