-3

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

Rajesh
  • 24,354
  • 5
  • 48
  • 79
Sukrit Gupta
  • 438
  • 4
  • 18

1 Answers1

2
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.

  • A useful reference: [JSON parse - single quote inside name](http://stackoverflow.com/questions/8012721/json-parse-single-quote-inside-name) – Rajesh Oct 21 '16 at 07:14