Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JavaScript-Packer/1c1944cd64f57a8569d1 to your computer and use it in GitHub Desktop.
Save JavaScript-Packer/1c1944cd64f57a8569d1 to your computer and use it in GitHub Desktop.
Escape HTML entities, JS safe escaping, URL encoding, etc. SOme functions used on http://www.whak.ca/packer/LZMA.htm
function esc_HTML(n) {
return n.replace(/[&\n\r "'<>\v\0]/g, function(n) {
return "&#" + n.charCodeAt(0) + ";";
});
}
function esc_URL(n) {
return n.replace(/[^\w\d]/g, function(n) {
return "%" + function(n, r, t) {
return t = t || "0", n += "", n.length < r ? Array(r - n.length + 1).join(t) + n : n;
}(n.charCodeAt(0).toString(16).toUpperCase(), 2, 0);
});
}
function esc_JS_octal(n) {
return n.replace(/[^\w\d]/g, function(n) {
return "\\" + function(n, r, t) {
return t = t || "0", n += "", n.length < r ? Array(r - n.length + 1).join(t) + n : n;
}(n.charCodeAt(0).toString(8).toUpperCase(), 2, 0);
});
}
function esc_JS_hex(n) {
return n.replace(/[^\w\d]/g, function(n) {
return "\\x" + function(n, r, t) {
return t = t || "0", n += "", n.length < r ? Array(r - n.length + 1).join(t) + n : n;
}(n.charCodeAt(0).toString(16).toUpperCase(), 2, 0);
});
}
function esc_codepoint(n) {
return n.replace(/[^]/g, function(n) {
return "" + function(n, r, t) {
return t = t || "0", n += "", n.length < r ? Array(r - n.length + 1).join(t) + n : n;
}(n.charCodeAt(0), 2, 0) + " ";
}).trim().replace(/ /g, ",");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment