Error Solved

Finally I solved the error

Javascript CSV parser

leave a comment »


var expr = /,(?=(?:[^\"]*\”[^\"]*\”)*(?![^\"]*\”))/g;
var array = str.split(expr);

it works for me


function parseLineCSV(lineCSV) {
// parse csv line by line into array
var CSV = new Array();

// Insert space before character ",". This is to anticipate 'split' in IE
// try this:
//
// var a=",,,a,,b,,c,,,,d";
// a=a.split(/\,/g);
// document.write(a.length);
//
// You will see unexpected result!
//
lineCSV = lineCSV.replace(/,/g," ,");

lineCSV = lineCSV.split(/,/g);

// This is continuing of 'split' issue in IE
// remove all trailing space in each field
for (var i=0;i
<lineCSV.length;i++) {
lineCSV[i] = lineCSV[i].replace(/\s*$/g,"");
}

lineCSV[lineCSV.length-1]=lineCSV[lineCSV.length-1].replace(/^\s*|\s*$/g,"");
var fstart = -1;

for (var i=0;i=0) {
for (var j=fstart+1;j<=i;j++) {
lineCSV[fstart]=lineCSV[fstart]+","+lineCSV[j];
lineCSV[j]="-DELETED-";
}
fstart=-1;
}
}
fstart = (lineCSV[i].match(/^"/)) ? i : fstart;
}

var j=0;

for (var i=0;i
<lineCSV.length;i++) {
if (lineCSV[i]!="-DELETED-") {
CSV[j] = lineCSV[i];
CSV[j] = CSV[j].replace(/^\s*|\s*$/g,""); // remove leading & trailing space
CSV[j] = CSV[j].replace(/^"|"$/g,""); // remove " on the beginning and end
CSV[j] = CSV[j].replace(/""/g,'"'); // replace "" with "
j++;
}
}

return CSV;
}

It works for me, I got it from here http://purbayubudi.wordpress.com/2008/11/09/csv-parser-using-javascript/

Written by daringtakers

February 10, 2009 at 1:41 pm

Leave a Reply