line breaks using javascript strings

23,970

Solution 1

var newText = text.replace(/\n/g , "<br>");

Solution 2

use double quotes to not escape the \n

try this:

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

Solution 3

/\r\n|[\r\n]/g <- this is what I use. I am a pessimist..

Share:
23,970
Pradyut Bhattacharya
Author by

Pradyut Bhattacharya

I am a boy... nothing else...

Updated on July 05, 2020

Comments

  • Pradyut Bhattacharya
    Pradyut Bhattacharya almost 4 years

    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