elena1234

how to escape HTML ( JavaScript)

Nov 5th, 2021 (edited)
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function escapeHtml(str) {
  2.     // html escape from Underscore.js https://coderwall.com/p/ostduq/escape-html-with-javascript
  3.     let entityMap = {
  4.         "&": "&",
  5.         "<": "&lt;",
  6.         ">": "&gt;",
  7.         '"': '&quot;',
  8.         "'": '&#39;',
  9.         "/": '&#x2F;'
  10.     };
  11.     return str.toString().replace(/[&<>"'\/]/g, (s) => entityMap[s]);
  12. }
Add Comment
Please, Sign In to add comment