Created
November 5, 2011 14:55
-
-
Save ecarter/1341624 to your computer and use it in GitHub Desktop.
encode html in coffeescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encode html | |
escape = (text) -> | |
replacements = | |
[/&/g, '&'] | |
[/</g, '<'] | |
[/"/g, '"'] | |
[/'/g, '''] | |
for r in replacements | |
text.replace r[0], r[1] |
Here is a shorter version that does the same work:
#encode html
escape = (text) ->
txt.replace(/&/g,'&' ).replace(/</g,'<').
replace(/"/g,'"').replace(/'/g,''')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For three reasons, this doesn't work as written:
The corrected code looks like this: