Skip to content

Instantly share code, notes, and snippets.

@keenwon
Created December 1, 2014 14:43
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 keenwon/6d99a764a12aa41d324a to your computer and use it in GitHub Desktop.
Save keenwon/6d99a764a12aa41d324a to your computer and use it in GitHub Desktop.
javascript安全字符串,参考Handlebars.js的实现
function safeString(string) {
if(!string) {
return "";
}
var escape = {
"&": "&",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#x27;",
"`": "&#x60;"
};
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