user11509999
user11509999

Reputation:

convert an string to a array in javascript?

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

Answers (2)

Girish Sasidharan
Girish Sasidharan

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

Majed Badawi
Majed Badawi

Reputation: 28414

Try this:

var strng = "[a,b]";
let arr = strng.slice(1,-1).split(",");
console.log(arr);

Upvotes: 0

Related Questions