1

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

 var strng = "[a,b]"

Expected output:

 var arry = ["a","b"]
asked Jun 20, 2020 at 9:53

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(","));

answered Jun 20, 2020 at 10:04
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);

answered Jun 20, 2020 at 9:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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