Skip to content

Instantly share code, notes, and snippets.

@brianonn
Last active January 2, 2016 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianonn/221ed49a22b772e73e86 to your computer and use it in GitHub Desktop.
Save brianonn/221ed49a22b772e73e86 to your computer and use it in GitHub Desktop.
JS: escape XML
//credits zzzzBov http://stackoverflow.com/questions/7918868/how-to-escape-xml-entities-in-javascript
if (!String.prototype.encodeHTML) {
String.prototype.encodeHTML = function () {
return this.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;');
};
}
if (!String.prototype.decodeHTML) {
String.prototype.decodeHTML = function () {
return this.replace(/&apos;/g, "'")
.replace(/&quot;/g, '"')
.replace(/&gt;/g, '>')
.replace(/&lt;/g, '<')
.replace(/&amp;/g, '&');
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment