javascript - 正则表达式除了在 , 之后尾随一个空格外
问题描述
我正在使用 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.");
}
}
解决方案
如果字段可能包含您的示例中的逗号,我建议更改此行
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)
")"
推荐阅读
- token - 星云认证注销
- java - 像导航抽屉一样切换片段(带后退箭头)
- c++ - C++ - 如何遍历数组的所有组合以进行暴力破解
- python - /blog/addblog 处的 ValueError
- dictionary - 传单:如何在鼠标悬停在该标记上时仅突出显示位于标记后面的多边形?
- android - 安卓机房:自动查询代码不校验完整性
- json - 在颤动中使用 Chopper 库将 JSON 响应转换为模型对象?
- php - 如何在 MacOS catalina、Xampp VM 和 php7.3 中安装 php_gmp
- serialization - Rust bincode enum 二进制序列化
- javascript - mysql pool.getConnection 失败会破坏代码