Skip to content

Instantly share code, notes, and snippets.

@kustomzone
Forked from alexgibson/specialChars.js
Last active March 17, 2017 15:50
Show Gist options
  • Save kustomzone/78087de49d08d12114f7edddb51ce0eb to your computer and use it in GitHub Desktop.
Save kustomzone/78087de49d08d12114f7edddb51ce0eb to your computer and use it in GitHub Desktop.
encode & decode special chars
//encodes special characters
function encodeString(str) {
return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;').replace(/\//g, '&#x2F;').replace(/=/g, '&#x3D;');
}
//decodes special characters
function decodeString(str) {
return str.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&#x27;/g, '\'').replace(/&#x2F;/g, '/').replace(/&#x3D;/g, '=');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment