This module addresses the problem of matching the fields in Microsoft’s Comma Separated Value (CSV) format.
This problem is addressed in Jeffrey Friedl’s book Mastering Regular Expressions, pg. 213
and you can compare this solution to his discussion there.
Two separate approaches are taken.
The first is to define a grammar that will match only one field, and then find them all in a loop with the global flag set.
The second is to define a grammar that scoops up all fields in one fell swoop.
The Microsoft format calls for escaping a quote within a quoted string by using a pair of quotes.
Therefore, this is not entirely just a pattern matching problem.
Regardless of whether using
a replacement process (translation) must follow the pattern matching (parsing) process.
However, I will also show how it can be almost as easily done with
The only thing that makes
var apgexp = require("apg-exp");
var apglib = require("apg-lib");
var grammar1 = new (require("./grammars/csv-fields.js"))();
var grammar2 = new (require("./grammars/csv.js"))();
var exp, str, rstr, result, value;
str = 'Ten Thousand,10000, 2710 ,,"10,000","It\'s ""10 Grand"", baby",10K';
exp = new apgexp(grammar1, "g");
console.log("the single-field grammar:");
console.log("the input string:");