Skip to content

Instantly share code, notes, and snippets.

@plbowers
Forked from Jezternz/csvStringToArray.js
Last active September 9, 2022 07:55
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save plbowers/7560ae793613ee839151624182133159 to your computer and use it in GitHub Desktop.
Save plbowers/7560ae793613ee839151624182133159 to your computer and use it in GitHub Desktop.
forked and (1) added header capability and (2) added escaped characters more consistently for https://stackoverflow.com/questions/1293147/javascript-code-to-parse-csv-data
const csvStringToArray = (strData, header=true) =>
{
//const objPattern = new RegExp(("(\\,|\\r?\\n|\\r|^)(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|([^\\,\\r\\n]*))"),"gi");
const objPattern = new RegExp(("(\\,|\\r?\\n|\\r|^)(?:\"((?:\\\\.|\"\"|[^\\\\\"])*)\"|([^\\,\"\\r\\n]*))"),"gi");
let arrMatches = null, arrData = [[]];
while (arrMatches = objPattern.exec(strData)){
if (arrMatches[1].length && arrMatches[1] !== ",") arrData.push([]);
arrData[arrData.length - 1].push(arrMatches[2] ?
arrMatches[2].replace(new RegExp( "[\\\\\"](.)", "g" ), '$1') :
arrMatches[3]);
}
if (header) {
hData = arrData.shift();
hashData = arrData.map(row => {
let i = 0;
return hData.reduce(
(acc, key) => {
acc[key] = row[i++];
return acc;
},
{}
);
});
return hashData;
} else {
return arrData;
}
}
<html>
<body>
<input type="checkbox" name="hasHeader" id="hasHeader" value="1" checked="checked" />Has Header<br />
<textarea id="foo" name="foo" cols="80" rows="10" />
h1,h2,h3,h4,h5
a,b,c,d,e
a,b,,d,
first,"second
with
multi-line",third,fourth,fifth
1,2,3,4,5,extra field that is ignored/lost if using headers
g,"h with ""embedded quotes"" asdf",i,j,k
l,"m with \"escaped quotes\" asdf",n,o,p
q,"r with \,escaped \,commas asdf",s,t,u
x,y,z</textarea>
<button onclick="doIt()">doIt</button>
<h3>Results</h3>
<div id="results">
</div>
</body>
<script>
const csvStringToArray = (strData, header=true) =>
{
//const objPattern = new RegExp(("(\\,|\\r?\\n|\\r|^)(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|([^\\,\\r\\n]*))"),"gi");
const objPattern = new RegExp(("(\\,|\\r?\\n|\\r|^)(?:\"((?:\\\\.|\"\"|[^\\\\\"])*)\"|([^\\,\"\\r\\n]*))"),"gi");
let arrMatches = null, arrData = [[]];
while (arrMatches = objPattern.exec(strData)){
if (arrMatches[1].length && arrMatches[1] !== ",") arrData.push([]);
arrData[arrData.length - 1].push(arrMatches[2] ?
arrMatches[2].replace(new RegExp( "[\\\\\"](.)", "g" ), '$1') :
arrMatches[3]);
}
if (header) {
hData = arrData.shift();
hashData = arrData.map(row => {
let i = 0;
return hData.reduce(
(acc, key) => {
acc[key] = row[i++];
return acc;
},
{}
);
});
return hashData;
} else {
return arrData;
}
}
function doIt() {
var h = document.getElementById('hasHeader');
var v = document.getElementById('foo');
var r = document.getElementById('results');
//console.log(h, h.checked);
var a = csvStringToArray(v.value, h.checked);
r.innerHTML = JSON.stringify(a, function(k, v) { return v === undefined ? '<i>(undefined)</i>' : v; }).replace(/},{/g, '},<br />\n{');
console.log(a);
}
</script>
</html>
@fawazahmed0
Copy link

Doesn't work, try this:
37,2,30,"30.またあなたの主が(先に)天使たちに向かって,「本当にわれは,地上に代理者を置くであろう。」と仰せられた時を思い起せ。かれらは申し上げた。「あなたは地上で悪を行い,血を流す者を置かれるのですか。わたしたちは,あなたを讃えて唱念し,またあなたの神聖を譲美していますのに。」かれは仰せられた。「本当にわれはあなたがたが知らないことを知っている。」",""

This https://gist.github.com/Jezternz/c8e9fafc2c114e079829974e3764db75 works fine

@fawazahmed0
Copy link

For some reason, this https://gist.github.com/Jezternz/c8e9fafc2c114e079829974e3764db75 becomes unresponsive, I ended up using papa parse https://www.papaparse.com/ , works great on large csv dataset

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment