Created
June 2, 2014 20:23
-
-
Save kfriend/a377c70a6fd7001718bf to your computer and use it in GitHub Desktop.
JavaScript: HTML encoder
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
escapeHTML = (function() { | |
var entities = { | |
'&': '&', | |
'<': '<', | |
'>': '>', | |
'"': '"', | |
"'": ''', | |
'/': '/' | |
}; | |
var regex = /[&<>"'\/]/g; | |
return function(string) { | |
return ('' + string).replace(regex, function(match) { | |
return entities[match]; | |
}); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment