为什么这个CSV文件没有解析为数组?

时间:2014-03-09 10:01:08

标签: javascript arrays parsing csv

我正在尝试将外部csv文件解析为javascript数组,它采用如下数据:

AL185,YAHOO/PA_AL185,1855
ATUK,GOOG/LON_ATUK,@UK PLC 
408,YAHOO/SZ_000408,*STJG 
ATTY,GOOG/PINK_ATTY,1-800-Attorney Inc- 

然后把它变成这样的数组:

var stocks = [
    ["STI,GOOG/NYSE_STI,SunTrust Banks"],
    ["AAPL,GOOG/NASDAQ_AAPL,Apple Inc"]
];

上面的代码应该正确解析它,但我不知道它为什么不起作用

代码:

$('#test').submit(function (event) {
function CSVToArray(strData, strDelimiter) {
    // Check to see if the delimiter is defined. If not,
    // then default to comma.
    strDelimiter = (strDelimiter || ",");

    // Create a regular expression to parse the CSV values.
    var objPattern = new RegExp(
    (
    // Delimiters.
    "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +

    // Quoted fields.
    "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +

    // Standard fields.
    "([^\"\\" + strDelimiter + "\\r\\n]*))"),
        "gi");


    // Create an array to hold our data. Give the array
    // a default empty first row.
    var arrData = [
        []
    ];

    // Create an array to hold our individual pattern
    // matching groups.
    var arrMatches = null;


    // Keep looping over the regular expression matches
    // until we can no longer find a match.
    while (arrMatches = objPattern.exec(strData)) {

        // Get the delimiter that was found.
        var strMatchedDelimiter = arrMatches[1];

        // When the very first character of the data is a delimiter
        // (either field or row delimiter) the item *before* the delimiter
        // is an unquoted empty string. This empty string we need to add
        // before handling the delimiter (ub@abego.org)
        if (arrMatches.index == 0 && strMatchedDelimiter) {
            arrData[arrData.length - 1].push("");
        }

        // Check to see if the given delimiter has a length
        // (is not the start of string) and if it matches
        // field delimiter. If id does not, then we know
        // that this delimiter is a row delimiter.
        if (
        strMatchedDelimiter.length && (strMatchedDelimiter != strDelimiter)) {

            // Since we have reached a new row of data,
            // add an empty row to our data array.
            arrData.push([]);

        }


        // Now that we have our delimiter out of the way,
        // let's check to see which kind of value we
        // captured (quoted or unquoted).
        if (arrMatches[2]) {

            // We found a quoted value. When we capture
            // this value, unescape any double quotes.
            var strMatchedValue = arrMatches[2].replace(
            new RegExp("\"\"", "g"),
                "\"");

        } else {

            // We found a non-quoted value.
            var strMatchedValue = arrMatches[3];

        }


        // Now that we have our value string, let's add
        // it to the data array.
        arrData[arrData.length - 1].push(strMatchedValue);
    }

    // Return the parsed data.
    return (arrData);
}
var csv =
    'AL185,YAHOO/PA_AL185,1855'
    'AL185,YAHOO/PA_AL185,1855'
var array = CSVToArray(csv, "\n");
alert(array[0]);
event.preventDefault();
})

上述功能属于:http://www.bennadel.com/blog/1504-Ask-Ben-Parsing-CSV-Strings-With-Javascript-Exec-Regular-Expression-Command.htm

1 个答案:

答案 0 :(得分:0)

而不是传递控制字符(“\ n”)&将它插入你的正则表达式中,在它前面加一个\,传递它的REPRESENTATION(“\\ n”)并将它放在你的正则表达式中,而不是额外的前导。 (注意:要让SO显示2个斜杠,这就是你需要的,我必须在这个帖子的“源”中放3个。)