1

I'm trying to convert a string to an array in javascript?

 var strng = "[a,b]"

Expected output:

 var arry = ["a","b"]

2 Answers 2

1

Work for all string like "[a,b]" or "[a,b,c,d]"

var strng = "[a,b]";
console.log(strng.replace(/\[|]/g, "").split(","));

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

Comments

0

Try this:

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

Comments

Your Answer

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