Created
April 1, 2015 10:07
-
-
Save gaowhen/6c0437162e309619e7e4 to your computer and use it in GitHub Desktop.
safe string
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 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