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 regex
or apg-exp
,
a replacement process (translation) must follow the pattern matching (parsing) process.
In this simple case, it is easiest to just use the built-in, JavaScript string replacement functions.
However, I will also show how it can be almost as easily done with apg-exp
.
The only thing that makes apg-exp
slightly more inconvenient is that it is not built into the JavaScript language.
(function csv() {
try {
const grammar1 = new (require('./grammars/csv-fields'))();
const grammar2 = new (require('./grammars/csv'))();
const apgJs = require('apg-js');
const { apgExp } = apgJs;
let exp;
let rstr;
let result;
let value;
console.log();
console.log('Comma Separated Values (CSV) demonstration');
const str = 'Ten Thousand,10000, 2710 ,,"10,000","It\'s ""10 Grand"", baby",10K';
exp = new apgExp(grammar1, 'g');
console.log();
console.log('the single-field grammar:');
console.log(exp.source);
console.log();
console.log('the input string:');
console.log(str);