Created
October 22, 2014 00:25
-
-
Save cheng470/2bc630793cc2ef87355b to your computer and use it in GitHub Desktop.
为了保证页面输出安全,我们经常需要对一些特殊的字符进行转义,请写一个函数escapeHtml,将<, >, &, “进行转义
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function escapeHtml(str) { | |
return str.replace(/[<>"&]/g, function(match) { | |
switch (match) { | |
case "<": return "<"; | |
case ">": return ">"; | |
case "&": return "&"; | |
case "\"": return """; | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment