Is there a javascript equivalent of PHP's nl2br() function? I have to send the value of a textarea to a new window that will display the text of what was in the box, which is why I need to somehow convert newlines to breaks.
Thanks,
Greg
| SitePoint Sponsor |
Is there a javascript equivalent of PHP's nl2br() function? I have to send the value of a textarea to a new window that will display the text of what was in the box, which is why I need to somehow convert newlines to breaks.
Thanks,
Greg
There's nothing built-in, but you can write your own nl2br function:
Code:function nl2br(someText) { return someText.replace("\n", "<br />"); }
Thanks!
That seemed to work, but for the first linefeed only. I figured out the solution is to make a loop that repeats the function until the end of the textarea.



A better solution than a loop would be to use a Regular Expression with the g (global) and m (multi-line) parameter:Originally Posted by Gregory
Code:function nl2br(someText) { return someText.replace ( /\n/gm, '<br />' ); }
Of course, that's just my opinion. I could be wrong.
Ahh...that is better! Thanks, Mr. Brownstone.
I use theses
Code:String.prototype.nl2br = function() { return this.replace( /\n/g, "<br />" ); }Code:String.prototype.br2nl = function() { return this.replace( /\<br ?/?\>/g, "\n" ); }


oh, you and your prototypes. when will you ever be satisfied.![]()
-ChaCha
...only when he owns the complete set![]()
::: certified wild guess :::
I'll send you the frisbeeOriginally Posted by beetle
![]()
Bookmarks