Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

line breaks using javascript strings

I m breaking a line in javascript using \n for example: -

text = "this is a test" + "\n" + "another test";

and to show that in html i m using the code

text = text.replace('\n' , '<br>');

but the text i m getting is without the br

when checking in firebug console i get is with line break(\n not shown but newline created)

how can i place line breaks using \n or i have to do some custom code instead of \n

thanks

like image 397
Pradyut Bhattacharya Avatar asked Dec 03 '25 09:12

Pradyut Bhattacharya


2 Answers

var newText = text.replace(/\n/g , "<br>");
like image 151
Babiker Avatar answered Dec 05 '25 22:12

Babiker


use double quotes to not escape the \n

try this:

text = text.replace("\n" , "<br>");
like image 43
jondavidjohn Avatar answered Dec 05 '25 23:12

jondavidjohn