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
Pratik
11.8k22 gold badges71 silver badges99 bronze badges
asked Dec 29, 2010 at 21:28
Pradyut Bhattacharya
5,75813 gold badges58 silver badges86 bronze badges
3 Answers 3
var newText = text.replace(/\n/g , "<br>");
answered Dec 29, 2010 at 21:30
Babiker
18.9k28 gold badges83 silver badges127 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Pradyut Bhattacharya
O O thanks.. regex.. somethings cannot be done without regex..:D
Pradyut Bhattacharya
can u please take a look at this question... stackoverflow.com/questions/4558136/…
Babiker
@Pradyut - It can be done with-out regex using a loop, regex is just easier.
Pradyut Bhattacharya
ya i m using a loop for getting the urls one by one for checking... that can be done with regex..thats wat the question is about simplying using regex...please help...
use double quotes to not escape the \n
try this:
text = text.replace("\n" , "<br>");
answered Dec 29, 2010 at 21:31
jondavidjohn
62.5k21 gold badges121 silver badges159 bronze badges
Comments
/\r\n|[\r\n]/g <- this is what I use. I am a pessimist..
Comments
lang-js