Javascript

escape html function

지네딘 주안 2020. 8. 6. 17:33

https://coderwall.com/p/ostduq/escape-html-with-javascript

 

Escape HTML with Javascript (Example)

A protip by gohan about escape, html, and javascript.

coderwall.com

 

(function(){
  "use strict";

  function escapeHtml() {
    return this.replace(/[&<>"'\/]/g, function (s) {
      var entityMap = {
          "&": "&amp;",
          "<": "&lt;",
          ">": "&gt;",
          '"': '&quot;',
          "'": '&#39;',
          "/": '&#x2F;'
        };

      return entityMap[s];
    });
  }

  if (typeof(String.prototype.escapeHtml) !== 'function') {
    String.prototype.escapeHtml = escapeHtml;
  }
})();

굉장하다..