Skip to content

Instantly share code, notes, and snippets.

@MartinSvarrer
Last active December 28, 2015 15:59
Show Gist options
  • Save MartinSvarrer/7525846 to your computer and use it in GitHub Desktop.
Save MartinSvarrer/7525846 to your computer and use it in GitHub Desktop.
Escape text. And make sure whitespace and line breaks are preserved. This should output the same result as a user input in a textarea. This will work in IE7 and newer, unlike whitespace: pre-wrap. Snippet is using the underscore libs escape method.
var escapeText = _.escape(insertText),
preserveWhiteSpace = escapeText.replace(/ /g, '\u00a0'),
preserveWhiteSpaceAndLineBreaks = preserveWhiteSpace.replace(/\n/g, '<br />');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment