nl2br javascript

Solutions on MaxInterview for nl2br javascript by the best coders in the world

showing results for - "nl2br javascript"
Henry
14 Mar 2016
1function nl2br (str, is_xhtml) {
2  // http://kevin.vanzonneveld.net
3  // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
4  // +   improved by: Philip Peterson
5  // +   improved by: Onno Marsman
6  // +   improved by: Atli Þór
7  // +   bugfixed by: Onno Marsman
8  // +      input by: Brett Zamir (http://brett-zamir.me)
9  // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
10  // +   improved by: Brett Zamir (http://brett-zamir.me)
11  // +   improved by: Maximusya
12  // *     example 1: nl2br('Kevin\nvan\nZonneveld');
13  // *     returns 1: 'Kevin<br />\nvan<br />\nZonneveld'
14  // *     example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
15  // *     returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
16  // *     example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
17  // *     returns 3: '<br />\nOne<br />\nTwo<br />\n<br />\nThree<br />\n'
18  var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br ' + '/>' : '<br>'; // Adjust comment to avoid issue on phpjs.org display
19
20  return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
21}
22
Louna
19 Mar 2019
1function nl2br (str, is_xhtml) {
2    if (typeof str === 'undefined' || str === null) {
3        return '';
4    }
5    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
6    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
7}