不知道这是否有帮助,但如果有帮助,请参阅http: //www.randomactsofsentience.com/2012/04/csv-handling-in-javascript.html...
附加:除了上面链接的解决方案(我的偏好)之外,我还使用了大量堆叠的正则表达式来标记 CSV,但对于自定义错误状态进行修改并不容易......
看起来很重,但仍然只需要几毫秒:
function csvSplit(csv){
csv = csv.replace(/\r\n/g,'\n')
var rows = csv.split("\n");
for (var i=0; i<rows.length; i++){
var row = rows[i];
rows[i] = new Array();
row = row.replace(/&/g, "&");
row = row.replace(/\\\\/g, "\");
row = row.replace(/\\"/g, """);
row = row.replace(/\\'/g, "'");
row = row.replace(/\\,/g, ",");
row = row.replace(/@/g, "@");
row = row.replace(/\?/g, "?");
row = row.replace(/"([^"]*)"/g, "@$1\?");
while (row.match(/@([^\?]*),([^\?]*)\?/)){
row = row.replace(/@([^\?]*),([^\?]*)\?/g, "@$1,$2?");
}
row = row.replace(/[\?@]/g, "");
row = row.replace(/\'([^\']*)\'/g, "@$1\?");
while (row.match(/@([^\?]*),([^\?]*)\?/)){
row = row.replace(/@([^\?]*),([^\?]*)\?/g, "@$1,$2?");
}
row = row.replace(/[\?@]/g, "");
row = row.split(",")
for (var j=0; j<row.length; j++){
col = row[j];
col = col.replace(/?/g, "\?");
col = col.replace(/@/g, "@");
col = col.replace(/,/g, ",");
col = col.replace(/'/g, '\'');
col = col.replace(/"/g, '\"');
col = col.replace(/\/g, '\\');
col = col.replace(/&/g, "&");
row[j]=col;
}
rows[i] = row;
}
return rows;
}