var strDate = "['a','b'],['c','d'],['e','f']";
//eval("([" + strDate + "])");console.log(str_to_arr(strDate));
functionstr_to_arr(str){
var a='',b=[];
s = str.split('],[');
//console.log(s.length);for(var i = 0;i < s.length;i++){
//console.log(s[i]);if(i==0){
a = s[i].substr(1);
a = a.replace(/\'/g, "");
a = a.replace(/\"/g, "");
a = a.split(",");
//console.log(a);
}elseif(i == Number(s.length-1)){
//console.log('del');
a = s[i].substr(0,s[i].length-1);
a = a.replace(/\'/g, "");
a = a.replace(/\"/g, "");
a = a.split(",");
//console.log(a);
}else{
a = s[i]
a = a.replace(/\'/g, "");
a = a.replace(/\"/g, "");
a = a.split(",");
//console.log(a);
}
b.push(a);
}
return b;
}
自己写的一个函数,不知道大佬有没有更好的办法
var strDate = "['a','b'],['c','d'],['e','f']"; //eval("([" + strDate + "])"); console.log(str_to_arr(strDate)); function str_to_arr(str){ var a='',b=[]; s = str.split('],['); //console.log(s.length); for(var i = 0;i < s.length;i++){ //console.log(s[i]); if(i==0){ a = s[i].substr(1); a = a.replace(/\'/g, ""); a = a.replace(/\"/g, ""); a = a.split(","); //console.log(a); }else if(i == Number(s.length-1)){ //console.log('del'); a = s[i].substr(0,s[i].length-1); a = a.replace(/\'/g, ""); a = a.replace(/\"/g, ""); a = a.split(","); //console.log(a); }else{ a = s[i] a = a.replace(/\'/g, ""); a = a.replace(/\"/g, ""); a = a.split(","); //console.log(a); } b.push(a); } return b; }
strDate.match(/\[.+?\]/g).map(e => e.replace(/\[|'|\]/g,'').split(','))
正则 match