Replace Newlines with BR, String Prototype


/ Published in: JavaScript
Save to your folder(s)

This will need to be either embeded or included and you can then just use this as such

myNewString = myStringVariable.nl2br();


Copy this code and paste it in your HTML
  1. function nl2br(){
  2.  
  3. var text = escape(this);
  4. var re_nlchar = null;
  5.  
  6. if(text.indexOf('%0D%0A') > -1)
  7. {
  8. re_nlchar = /%0D%0A/g ;
  9. }
  10. else if(text.indexOf('%0A') > -1)
  11. {
  12. re_nlchar = /%0A/g ;
  13. }
  14. else if(text.indexOf('%0D') > -1)
  15. {
  16. re_nlchar = /%0D/g ;
  17. }
  18.  
  19. text = (re_nlchar != null) ? unescape(text.replace(re_nlchar,'<br />')) : unescape(text);
  20. return text
  21. }
  22.  
  23. String.prototype.nl2br = nl2br;

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.