.split function with separator value in variable not working

I have a method to import data from any CSV file. One must first define the parameters to recognise records and fields. To chose the record separator I have made a value list with two possible choices, LF (\n) or CR (\r\n)

//valuelist
Unix LF|\n
Windows CR|\r\n

The selected value is then passed to the method to convert the string to an array using function .split(separator,limit)

//var recSep contains value from valuelist 
//var csvTxt contains string from csv file
var arrCsv = new Array();
arrCsv = csvTxt.split(recSep);

The problem is that the method doesn’t seem to recognise the record separator when this is generated by the value list, while it works if I substitute the variable with a string or with another variable not initiated by the value list. At first I thought it was a problem with the escape character: so I added one, two and three slashes in front of “\n”. But I always get odd results. I found an easy workaround by passing ‘LF’ or ‘CR’ from the value list triggering a method firing the relevant .split metod. But i am just curious to understand why the first metod doesn’t work. Thank you for any help.

Try creating a RegExp object instead of using a String, this will eliminate the need to escape characters (in JavaScript and then in Java).
Also no need to create a new Array() to replace it with a new one one the line below ;)

So simply:

var arrCsv = csvTxt.split(/\r\n|\n|\r/g);

Should work fine. It does for me here.

Excellent, thanks. It works. Moreover, I learned that one could use a RegExp instead of a string in a function like this. Good to know.

Thank you very much.