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/>"));
}); 
kxyz
  • 802
  • 1
  • 9
  • 32

1 Answers1

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/>") );
});
Francois
  • 2,005
  • 21
  • 39