Last active
August 26, 2016 20:30
-
-
Save gpawlik/bafb6fdd187d0a27770db48ffb2fea43 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parseComplexCSV(input, separator, quote) { | |
separator = separator || ','; | |
quote = quote || '"'; | |
return input | |
.replace(new RegExp('\\'+quote+'\\'+quote,'g'), 'doubleQuoteFlag') | |
.split(quote) | |
.map(function(val, index) { | |
return index%2 ? | |
val.replace(new RegExp('doubleQuoteFlag','g'), quote) : | |
val.replace(new RegExp('\n','g'),'splitByMeFirst') | |
.replace(new RegExp('\\' + separator,'g'),'splitByMeSecond') | |
.replace(new RegExp('doubleQuoteFlag','g'), ''); | |
}) | |
.join('') | |
.split('splitByMeFirst') | |
.map(function(val) { | |
return val.split('splitByMeSecond'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test cases: