Created
May 15, 2019 03:53
-
-
Save vaibhavpandeyvpz/ff89a6ef79ba26b37055f8bd867d551e to your computer and use it in GitHub Desktop.
Escape HTML tags in JS before rendering to DOM.
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
const e = str => { | |
const replacements = { | |
'&': '&', | |
'<': '<', | |
'>': '>', | |
"'": ''', | |
'"': '"', | |
}; | |
return str.replace(/[&<>"']/g, match => replacements[match]) | |
}; | |
export default e; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment