From this question, this ...
lines = foo.value.split[/\r\n|\r|\n/];
is one way to split a string, but how do I join it back with newlines?
Also, I wonder if I is say linux which uses whichever newline character, then switch to windows, won't my web app break? The newlines become not recognized? Or maybe the browser does some conversion?
asked Dec 1, 2010 at 3:54
Jiew MengJiew Meng
82.4k174 gold badges476 silver badges788 bronze badges
If you want to join using newline characters, just do:
lines.join["\r\n"];
But if you want to display on the HTML page, you'd want to wrap each line in tags:
html = "" + lines.join["
"] + "
";
answered Dec 1, 2010 at 3:59
David TangDavid Tang
90.4k29 gold badges165 silver badges149 bronze badges
2
You can use the Array
object's join
method to glue together array elements into a string:
lines.join["\r\n"];
In CSS: remember to use
white-space: pre;
Syntle
5,0883 gold badges12 silver badges34 bronze badges
answered Dec 1, 2010 at 3:56
Jacob RelkinJacob Relkin
158k32 gold badges341 silver badges318 bronze badges
2
Split it on /\r?\n/, in case the string includes the carriage returns with newlines.
join it with '\n', in any browser and any os.
answered Dec 1, 2010 at 4:23
1
As said, join is the best, but here is the hard way [untested, I hope it's not too trivial]:
var result;
for [i=0;i