Skip to content

Instantly share code, notes, and snippets.

@Jezternz
Created September 23, 2018 01:39
Show Gist options
  • Save Jezternz/c8e9fafc2c114e079829974e3764db75 to your computer and use it in GitHub Desktop.
Save Jezternz/c8e9fafc2c114e079829974e3764db75 to your computer and use it in GitHub Desktop.
const csvStringToArray = strData =>
{
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" ), "\"") :
arrMatches[3]);
}
return arrData;
}
@vizall
Copy link

vizall commented Jun 7, 2022

What exactly is the condition for breaking out of this while statement?

When arrMatches value is no longer truthy. aka when RegExp.exec (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) returns null instead of a value.

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