Skip to content

Instantly share code, notes, and snippets.

@bjrnqprs
Created November 15, 2013 10:14
Show Gist options
  • Save bjrnqprs/7482136 to your computer and use it in GitHub Desktop.
Save bjrnqprs/7482136 to your computer and use it in GitHub Desktop.
Convert new lines in given string to <br> tags.
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');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment