Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: replace linebreak

I'm having a string which contains a chr(13) as linebreak. How can i replace it with eg. <br>? I tried mystring.replace("\n","<br>"); but it didn't work

Thanks in advance.

like image 605
Fuxi Avatar asked Feb 08 '10 22:02

Fuxi


1 Answers

"\n" is chr(10). I think you want "\r":

mystring.replace("\r", "<br>");

Updated: To replace ALL \r use a regular expression:

mystring.replace(/\r/g, "<br>");

If you want it to work with Windows, Unix and Mac style line breaks use this:

mystring.replace(/\r?\n|\r/g, "<br>");
like image 96
Mark Byers Avatar answered Oct 05 '22 15:10

Mark Byers