From 886fdf3cb29b87979a2b0d7ed722f9b89a37e215 Mon Sep 17 00:00:00 2001 From: Chris Zubak-Skees Date: Sun, 2 Dec 2018 14:16:23 -0500 Subject: [PATCH] Consistently apply regex escaping to quoteChar (#602) --- papaparse.js | 4 ++-- tests/test-cases.js | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/papaparse.js b/papaparse.js index f39a108..18715b8 100755 --- a/papaparse.js +++ b/papaparse.js @@ -296,7 +296,7 @@ if (!Array.isArray) unpackConfig(); - var quoteCharRegex = new RegExp(_quoteChar, 'g'); + var quoteCharRegex = new RegExp(escapeRegExp(_quoteChar), 'g'); if (typeof _input === 'string') _input = JSON.parse(_input); @@ -1438,7 +1438,7 @@ if (!Array.isArray) var nextDelim = input.indexOf(delim, cursor); var nextNewline = input.indexOf(newline, cursor); - var quoteCharRegex = new RegExp(escapeChar.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&') + quoteChar, 'g'); + var quoteCharRegex = new RegExp(escapeRegExp(escapeChar) + escapeRegExp(quoteChar), 'g'); var quoteSearch; // Parser loop diff --git a/tests/test-cases.js b/tests/test-cases.js index 98509de..b596568 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -1324,7 +1324,7 @@ var PARSE_TESTS = [ } }, { - description: "Using reserved regex characters as quote characters", + description: "Using reserved regex character . as quote character", input: '.a\na.,b\r\nc,d\r\ne,f\r\ng,h\r\ni,j', config: { quoteChar: '.' }, expected: { @@ -1339,6 +1339,22 @@ var PARSE_TESTS = [ } } }, + { + description: "Using reserved regex character | as quote character", + input: '|a\na|,b\r\nc,d\r\ne,f\r\ng,h\r\ni,j', + config: { quoteChar: '|' }, + expected: { + data: [['a\na', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'j']], + errors: [], + meta: { + linebreak: '\r\n', + delimiter: ',', + cursor: 27, + aborted: false, + truncated: false + } + } + }, { description: "Parsing with skipEmptyLines set to 'greedy'", notes: "Must parse correctly without lines with no content",