Skip to content

Instantly share code, notes, and snippets.

@mocheng
Created May 10, 2011 02:38
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 mocheng/963826 to your computer and use it in GitHub Desktop.
Save mocheng/963826 to your computer and use it in GitHub Desktop.
Different Implementation of escapeHTML
function escapeHTML(s) {
return s.replace(/&/g, '&amp;').replace(/>/g, '&gt;').replace(/</g,'&lt;').replace(/"/g, '&quot').replace(/'/g,'&#x27;').replace(/\//g,'&#x2F;');
}
function escapeHTML(s) {
r={'&':'&amp;','>':'&gt;','<':'&lt;','"':'&quot;','\'':'&#x27;','/':'&#x2f'};
return s.replace(/./g, function(m){return r[m]?r[m]:m})
}
function escapeHTML(s) {
r='replace';return s[r](/&/g,'&amp;')[r](/>/g,'&gt;')[r](/</g,'&lt;')[r](/"/g, '&quot')[r](/'/g,'&#x27;')[r](/\//g,'&#x2F;');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment