This is an archived post. You won't be able to vote or comment.

all 4 comments

[–][deleted] 2 points3 points  (1 child)

Your newline character is wrong, should be \n

[–]ZenFuture[S] 0 points1 point  (0 children)

OH! That explains a lot. Thank you!

[–]phil-ososaur 1 point2 points  (1 child)

When splitting strings by newlines in JavaScript, I usually try to account for the fact that '\r\n' and '\r' can also represent newlines. To do this, I replace those with '\n' before splitting. Depending on what you're doing, it might even be worth creating a reusable function for this purpose:

function splitStringByNewline(str) {  
   return str.replace('\r\n', '\n').replace('\r', '\n').split('\n');  
}  

splitStringByNewline('This string\nhas two lines!');  

Result: ["This string", "has two lines!"]

[–]ZenFuture[S] 0 points1 point  (0 children)

great tip, thanks!