Last active
January 2, 2016 16:30
-
-
Save brianonn/221ed49a22b772e73e86 to your computer and use it in GitHub Desktop.
JS: escape XML
This file contains 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
//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, '<') | |
.replace(/>/g, '>') | |
.replace(/"/g, '"') | |
.replace(/'/g, '''); | |
}; | |
} | |
if (!String.prototype.decodeHTML) { | |
String.prototype.decodeHTML = function () { | |
return this.replace(/'/g, "'") | |
.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