Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I'm trying to convert a string to an array in javascript?
var strng = "[a,b]"
Expected output:
var arry = ["a","b"]
Work for all string like "[a,b]" or "[a,b,c,d]"
"[a,b]"
"[a,b,c,d]"
var strng = "[a,b]"; console.log(strng.replace(/\[|]/g, "").split(","));
Add a comment
Try this:
var strng = "[a,b]"; let arr = strng.slice(1,-1).split(","); console.log(arr);
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.