Skip to content

Instantly share code, notes, and snippets.

@ecarter
Created November 5, 2011 14:55
Show Gist options
  • Save ecarter/1341624 to your computer and use it in GitHub Desktop.
Save ecarter/1341624 to your computer and use it in GitHub Desktop.
encode html in coffeescript
# encode html
escape = (text) ->
replacements =
[/&/g, '&']
[/</g, '&lt;']
[/"/g, '&quot;']
[/'/g, '&#039;']
for r in replacements
text.replace r[0], r[1]
@Yaakov-Belch
Copy link

Here is a shorter version that does the same work:

#encode html
escape = (text) -> 
  txt.replace(/&/g,'&amp;' ).replace(/</g,'&lt;').
      replace(/"/g,'&quot;').replace(/'/g,'&#039;')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment