Sukrit Gupta Sukrit Gupta - 1 month ago 4
Javascript Question

Convert String to nested array of characters

I have a string passed to me like

var str = "['♫', ['►', 'Play']]";


Multiple entries can be there

Need to convert it to Array.

How can It be done?

PS: No eval() please. and also no Jquery

Note: I have tried JSON.parse, but it didn't work for me. It gives following error:


Unexpected token ' in JSON at position 1

Answer
JSON.parse(str.replace(/'/g, '"'))

In other words, fix the quotes so that it is valid JSON, then you will be able to use JSON.parse.