首页 > 解决方案 > 正则表达式除了在 , 之后尾随一个空格外

问题描述

我正在使用 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.");
    }

}

标签: javascriptjqueryregexcsvcsv-import

解决方案


如果字段可能包含您的示例中的逗号,我建议更改此行

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)
")"  

推荐阅读