How to split string with newline ('\n') in Node?
Within Node, how do I split a string using newline ('\n') ?
I have a simple string like var a = "test.js\nagain.js"
and I need to get ["test.js", "again.js"]
.
I tried
a.split("\n");
a.split("\\n");
a.split("\r\n");
a.split("\r");
but the above doesn't work.
Try splitting on a regex like /\r?\n/
to be usable by both Windows and UNIX systems.
> "a\nb\r\nc".split(/\r?\n/)
[ 'a', 'b', 'c' ]
If the file is native to your system (certainly no guarantees of that), then Node can help you out:
var os = require('os');
a.split(os.EOL);
This is usually more useful for constructing output strings from Node though, for platform portability.