Created
December 1, 2014 14:43
-
-
Save keenwon/6d99a764a12aa41d324a to your computer and use it in GitHub Desktop.
javascript安全字符串,参考Handlebars.js的实现
This file contains 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 safeString(string) { | |
if(!string) { | |
return ""; | |
} | |
var escape = { | |
"&": "&", | |
"<": "<", | |
">": ">", | |
'"': """, | |
"'": "'", | |
"`": "`" | |
}; | |
var badChars = /[&<>"'`]/g; | |
return string.replace(badChars, function(chr) { | |
return escape[chr]; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment