Nodejs HTML Encode encodeHTML()

Here you can find the source of encodeHTML()

Method Source Code

/**/*from w  w w  . ja va  2s .co m*/
 * Add HTML encode function to String.
 *
 * @returns {String} the HTML encoded string
 */
String.prototype.encodeHTML = function () {
    return this.replace(/&/g, '&')
           .replace(/</g, '&lt;')
           .replace(/>/g, '&gt;')
           .replace(/"/g, '&quot;');
};

Related

  1. encodeHTML()
    define(function() {
    String.prototype.encodeHTML=function () {
          var encodeHTMLRules, matchHTML;
          encodeHTMLRules = {
            "&": "&#38;",
            "<": "&#60;",
            ">": "&#62;",
            '"': '&#34;',
            "'": '&#39;',
    ...
    
  2. encodeHTML()
    String.prototype.encodeHTML = function() {
      var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;' },
          matchHTML = /&(?!#?\w+;)|<|>|"|'|\
      return function() {
        return this ? this.replace(matchHTML, function(m) {return encodeHTMLRules[m] || m;}) : this;
      };
    }();
    
  3. encodeHTML();
    function encodeHTMLSource() {
        var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;'},
            matchHTML = /&(?!#?\w+;)|<|>|"|'|\
        return function() {
            return this ? this.replace(matchHTML, function(m) {return encodeHTMLRules[m] || m; }) : this;
        };
    String.prototype.encodeHTML = encodeHTMLSource();
    
  4. encodeHtml()
    String.prototype.encodeHtml = function() {
     var encodedHtml = escape(this);
     encodedHtml = encodedHtml.replace(/\
     encodedHtml = encodedHtml.replace(/\?/g,"%3F");
     encodedHtml = encodedHtml.replace(/=/g,"%3D");
     encodedHtml = encodedHtml.replace(/&/g,"%26");
     encodedHtml = encodedHtml.replace(/@/g,"%40");
     return encodedHtml;