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

0

3 Answers 3

5
var newText = text.replace(/\n/g , "<br>");
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>");

Comments

3

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

Comments

Your Answer

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.