Skip to content

Instantly share code, notes, and snippets.

@JavaScript-Packer
Created July 5, 2015 23:58
Show Gist options
  • Select an option

  • Save JavaScript-Packer/bd38ff04d79553754432 to your computer and use it in GitHub Desktop.

Select an option

Save JavaScript-Packer/bd38ff04d79553754432 to your computer and use it in GitHub Desktop.
My take on HTML entity encoding, escape HTML source code, make XML safe, etc. Thought I needed it for http://www.whak.ca/HTMLZIP.htm but didn't, so here it is the script:
function html_entities(me) {
return me.replace(/^\s+|\s+$/g, "").replace(/(\r\n|\n|\r|\x0a)/gm, "\n").replace(/\n{2,}/g, "\n").replace(/\n/g, "&#10;").replace(/\0/g, "&#00;").replace(/\t{2,}/g, "\t").replace(/\x27/g, "&#39;").replace(/\x22/g, "&#34;").replace(/ {2,}/g, " ").replace(/</g, "&#60;").replace(/>/g, "&#62;");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment