Javascript String encodeHTML()

Description

Javascript String encodeHTML()


define(function() {

String.prototype.encodeHTML=function () {
      var encodeHTMLRules, matchHTML;
      encodeHTMLRules = {/*from   ww  w  .  jav  a2 s  .c  om*/
        "&": "&",
        "<": "&#60;",
        ">": "&#62;",
        '"': '&#34;',
        "'": '&#39;',
        "/": '&#47;'
      };
      matchHTML = /&(?!#?w+;)|<|>|"|'|\//g;
      return this.replace(matchHTML, function(m) {
        return encodeHTMLRules[m] || m;
      });
    };



PreviousNext

Related