PHP provides a useful function nl2br that inserts HTML line breaks before all newlines \n in a string. Many times we want to utilize this functionality in JavaScript, although there is no nl2br equivalent JavaScript function but we can create a custom function using regular expressions. Below is JavaScript equivalent for nl2br. function nl2br(str, is_xhtml) { var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>'; return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2'); }
JavaScript
nl2br equivalent JavaScript function
PHP provides a useful function nl2br that inserts HTML line breaks before all newlines \n in a string. Many times we want to utilize this functionality in JavaS
· 5 min read
Related posts
JavaScript
Toggle checkboxes with jQuery and Prototype JS
Sometimes you work with forms having a long list of checkboxes, in such scenarios a “Check All” or “Toggle Selection” button comes handy
JavaScript
JQuery Multi fileupload
File Upload widget with multiple file selection, drag&drop support, progress bars and preview images for jQuery. Supports cross-domain, chunked and resumabl
JavaScript
JQuery tooltip
Here is a nice jQuery tool tip. How to use: 1- Download the file 1.1- Rename the file tipsy-zip.doc to tipsy.zip 1.2- Extract the zip folder or download it from