3

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
0

3 Answers 3

5
var newText = text.replace(/\n/g , "<br>");
answered Dec 29, 2010 at 21:30
Sign up to request clarification or add additional context in comments.

4 Comments

O O thanks.. regex.. somethings cannot be done without regex..:D
can u please take a look at this question... stackoverflow.com/questions/4558136/…
@Pradyut - It can be done with-out regex using a loop, regex is just easier.
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...
3

use double quotes to not escape the \n

try this:

text = text.replace("\n" , "<br>");
answered Dec 29, 2010 at 21:31

Comments

3

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

answered Jul 5, 2013 at 5:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.