var str = ("['movies', ['The Wind', [7, 'acters', ['Any', 'Roberto']]]," +
"['Abc',['acters',['Max', 'Alex', 0.312, false, true]]]]").replace(/'/g, '"');
console.log(JSON.parse(str));
Replace ' with " and parse as json string.
ps: I broke it into two lines just for better readability.
pps: if you have something like [,, ,] or ,, replace .replace part with .replace(/'/g, '"').replace(/([\[,])\s*([\],])/g, '$1 "" $2');
ppps: ok, this is for your weird case
var str = "['movies', ['The Wind', [7, 'acters', ['Any', 'Roberto']]]," +
"['Abc',['acters',['Max', 'Alex', 0.312,, false, true,],[,],[,[,],]]]]";
str = fixJson(str.replace(/'/g, '"'));
console.log(JSON.parse(str));
function fixJson(match, m1, m2, m3) {
if (m1 && m2)
match = m1 + '""' + m2 + (m3 ? m3 : '');
return match.replace(/([\[,])\s*([\],])([\s\]\[,]+)?/g, fixJson);
}
pps#?: I wrote a symbol by symbol parser, but it is not complete: 1) it does not check the closure, but you can count number of [ and ] 2) ... probably something else ... like it trims all quotes, but can be fixed by modification of /^["'\s]+|["'\s]+$/g to /^\s*'|'\s*$/g, for example.
var str = "['movies', ['The Wind', [7, 'acters', ['Any', 'Roberto']]]," +
"['Abc',['acters',['Max', 'Alex', 0.312,, false, true,],[,],[,[,],]]],]";
var result = parse(str, 0);
console.log(result);
function parse(str, start)
{
var i = start, res = new Array(), buffer = '',
re = /^\s*'|'\s*$/g, quotes = false;
while(i < str.length) { // move along the string
var s = str[i];
switch(s) { // check the letter
case "'": // if quote is found, set the flag inside of element
if (buffer.substr(-1) != '\\') // check for escaping
quotes = !quotes;
else
buffer = buffer.substring(0, buffer.length - 1); // remove /
buffer += s;
break;
case '[': // opening [
if (!quotes) { // not inside of a string element
ret = parse(str, i + 1); // create subarray recursively
i = ret.pos; // get updated position in the string
buffer = null;
res.push(ret.ret); // push subarray to results
} else buffer += s; // add symbol if inside of a string
break;
case ']': // closing ]
if (!quotes) { // not inside of a string
if (buffer != null) // buffer is not empty
res.push(buffer.replace(re, "")); // push it to subarray
return {ret: res, pos: i}; // return subarray, update position
}
buffer += s; // if inside of a string - add letter
break;
case ',': // comma
if (!quotes) { // not inside of a string
if (buffer != null) // buffer is not empty
res.push(buffer.replace(re, "")); // push it to results
buffer = ''; // clear buffer
break;
}
default: buffer += s; break; // add current letter to buffer
}
i++;
}
return res;
}