Skip to content

Instantly share code, notes, and snippets.

@vaibhavpandeyvpz
Created May 15, 2019 03:53
Show Gist options
  • Save vaibhavpandeyvpz/ff89a6ef79ba26b37055f8bd867d551e to your computer and use it in GitHub Desktop.
Save vaibhavpandeyvpz/ff89a6ef79ba26b37055f8bd867d551e to your computer and use it in GitHub Desktop.
Escape HTML tags in JS before rendering to DOM.
const e = str => {
const replacements = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
"'": '&#039;',
'"': '&quot;',
};
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