我正在使用javascript导入CSV文件。我的文件中包含“ Advance Home Technologies,Inc.”之类的字符串,因此当我用“”分隔行时,它也会同时分隔“ Advance Home Technologies”和“ Inc.”。所以我想为此解决。下面是我的代码。
function UploadCSV() {
var csvFileUpload = document.getElementById("csvFileUpload");
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.csv|.txt)$/;
if (regex.test(csvFileUpload.value.toLowerCase())) {
if (typeof (FileReader) != "undefined") {
var reader = new FileReader();
reader.onload = function (e) {
var table = document.createElement("table");
var rows = e.target.result.split("\n");
for (var i = 1; i < rows.length; i++) {
var row = table.insertRow(-1);
var cells = rows[i].split(",");
for (var j = 0; j < cells.length; j++) {
var cell = row.insertCell(-1);
cell.innerHTML = cells[j];
}
}
var dvTable = document.getElementById("dvTable");
dvTable.value = "";
dvTable.appendChild(table);
document.getElementById("table_data").value = document.getElementById("dvTable").innerHTML;
}
reader.readAsText(csvFileUpload.files[0]);
} else {
alert("This browser does not support HTML5.");
}
//document.getElementById("table_data").value = document.getElementById("dvTable").value;
} else {
alert("Please upload a valid CSV file.");
}
}
答案 0 :(得分:1)
如果您的示例中的字段可能包含逗号,则建议更改此行
var cells = rows[i].split(",");
进入
cells = rows[i].split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/);
这会将逗号分隔行,除非它们位于带引号的字符串中。
正则表达式说明:
"," + Match the character “,” literally
"(?=" + Assert that the regex below can be matched, starting at this position (positive lookahead)
"(?:" + Match the regular expression below
"(?:" + Match the regular expression below
'[^"]' + Match any character that is NOT a “"”
"*" + Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
'"' + Match the character “"” literally
"){2}" + Exactly 2 times
")*" + Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
'[^"]' + Match any character that is NOT a “"”
"*" + Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
"$" + Assert position at the end of the string (or before the line break at the end of the string, if any)
")"