1

I'm trying to replace new lines in java script to <br>'s. But...

$(this).html($(this).text().replace(/(\n|\r)/gm, "<br>")); 

and the others function like this one replaced first line too. So I'm getting formatted text with enters at the start. Example

<br> (?) <br> (?) Text <br> 

How to solve this issue ?

$(".class").each(function(){ $(this).html($(this).text().replace(/\\n/g, "<br/>")); }); 
3
  • Have you tried .replace(/(\r\n|\n|\r)/gm,"<br/>")? What does So I'm getting formatted text with to enters at the start. mean? Commented Aug 5, 2014 at 11:02
  • With br's at the start, in database I dont have it Commented Aug 5, 2014 at 11:08
  • 1
    I guess this comment should help you out: stackoverflow.com/a/2919363/2328888 Commented Aug 5, 2014 at 11:14

1 Answer 1

1

You may need to remove the leading '\n' from the string, like this:

$(".class").each(function() { $(this).html( $(this).text().trim('\n').replace(/\n/g, "<br/>") ); }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.