Skip to content

Instantly share code, notes, and snippets.

@cheng470
Created October 22, 2014 00:25
Show Gist options
  • Save cheng470/2bc630793cc2ef87355b to your computer and use it in GitHub Desktop.
Save cheng470/2bc630793cc2ef87355b to your computer and use it in GitHub Desktop.
为了保证页面输出安全,我们经常需要对一些特殊的字符进行转义,请写一个函数escapeHtml,将<, >, &, “进行转义
function escapeHtml(str) {
return str.replace(/[<>"&]/g, function(match) {
switch (match) {
case "<": return "&lt;";
case ">": return "&gt;";
case "&": return "&amp;";
case "\"": return "&quot;";
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment