Reputation: 9763
I have been trying to solve this for far too long. Can someone explain why none of my attempts below will produce the value I expected?
goal is: transResult=[[xy,zy],[xz,zz]]
let allData='xy|zy\r\nxz|zz'
console.log('first split: '+allData.split(/\r?\n/)[0])
//first split: xy|zy
let transResult=allData.split(/\r?\n/).map(x=>x.split(/|/))
console.log(transResult)
//[ [ 'x', 'y', '|', 'z', 'y' ], [ 'x', 'z', '|', 'z', 'z' ] ]
If I split one element eg
console.log('xy|zy'.split('|'))
I get the expected value of [ 'xy', 'zy' ]
What is happening with the first map() that is screwing up the result in the first section of code?
Upvotes: 0
Views: 79
Reputation: 1943
|
is a special character so you have to escape it using \
:
const allData='xy|zy\r\nxz|zz';
const transResult=allData.split(/\r?\n/).map(x=>x.split(/\|/));
console.log(transResult);
Upvotes: 1