如果把字符串转成多维数组


例如我有一个字符串
var str = 'dog,[cat,lion],[bird,chicken,duck]';
我希望把他转成一个数组:
arr = [dog,[cat,lion],[bird,chicken,duck]];

应该怎么操作呢?困扰了一个晚上 T_T

正则表达式 javascript正则 JavaScript

琐琐碎碎2 10 years, 2 months ago

eval 也行,


 var str = 'dog,[cat,lion],[bird,chicken,duck]';
eval('['+str.replace(/\w+/g, function(a){return '"'+a+'"'})+']')

mizuka answered 10 years, 2 months ago


 var str = 'dog,[cat,lion],[bird,chicken,duck]';
JSON.parse('['+str.replace(/\w+/g, function(a){return '"'+a+'"'})+']')

谁盯谁怀孕 answered 10 years, 2 months ago

Your Answer