Blog

/**
 * nl2br.
 *
 * @param {String} str
 * @param {Boolean} replaceMode
 * @param {Boolean} isXhtml Use XHTML 
 *
 * @returns {String}
 */
function nl2br (str, replaceMode, isXhtml) {
  var breakTag = (isXhtml) ? '
' : '
'; var replaceStr = (replaceMode) ? '$1'+ breakTag : '$1'+ breakTag +'$2'; return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, replaceStr); }
/**
 * br2nl.
 *
 * @param {String} str
 * @param {Boolean} replaceMode

 * @returns {String}
 */
function br2nl (str, replaceMode) {   
  var replaceStr = (replaceMode) ? "\n" : '';
  return str.replace(/<\s*\/?br\s*[\/]?>/gi, replaceStr);
}

Optionally, you could add white-space style with pre-line:

$("div").css('white-space', 'pre-line').text('First Line\nSecond Line\nThird Line');
Zoeken