Repository: mholt/PapaParse (js)

Issue:
QuoteChar option in `unparse` blows up the parser if it's a regex special char
If you specify a regex special char as a quoteChar then the library blows up:

```
Papa.unparse('[{"a": "1", "b": "2", "c": "3"}]', { quoteChar: '(' })
```

> papaparse.min.js:6 Uncaught SyntaxError: Invalid regular expression: /(/: Unterminated group
>    at new RegExp (<anonymous>)
>    at Object.b [as unparse] (papaparse.min.js:6)
>    at <anonymous>:1:6



Pull request:
Consistently apply regex escaping to quoteChar
Closes #424 

Patch:
diff --git a/papaparse.js b/papaparse.js
index 66dba9d..7febbaf 100755
--- a/papaparse.js
+++ b/papaparse.js
@@ -278,7 +278,7 @@ License: MIT
 
 		unpackConfig();
 
-		var quoteCharRegex = new RegExp(_quoteChar, 'g');
+		var quoteCharRegex = new RegExp(escapeRegExp(_quoteChar), 'g');
 
 		if (typeof _input === 'string')
 			_input = JSON.parse(_input);
@@ -1417,7 +1417,7 @@ License: MIT
 
 			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


Test patch:
diff --git a/tests/test-cases.js b/tests/test-cases.js
index 69b9b25..2e19d68 100644
--- a/tests/test-cases.js
+++ b/tests/test-cases.js
@@ -1358,7 +1358,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: {
@@ -1373,6 +1373,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",