Nodejs HTML Escape htmlEscape()

Here you can find the source of htmlEscape()

Method Source Code

String.prototype.htmlEscape = function() {
  var obj = document.createElement('div');
  if (typeof obj.textContent != 'undefined') {
    obj.textContent = this;//from   w ww . j  a  va  2 s .  com
  } else {
    obj.innerText = this;
  }
  return obj.innerHTML;
};

Related

  1. htmlEscape()
    String.prototype.htmlEscape = function() {
         return $('<div/>').text(this.toString()).html();
    };
    (function($) {
        $.fn.hasScrollBar = function() {
            if (this.height() < 0) {
                return false;
            return this.get(0).scrollHeight > this.height();
    ...
    
  2. htmlEscape()
    String.prototype.htmlEscape = function (){
        var escapedStr = String(this).replace(/&/g, '&amp;');
        escapedStr = escapedStr.replace(/\s/g, '&nbsp;');
        return escapedStr;
    var text = 'This is for testing';
    console.log(text);
    text = text.htmlEscape(); 
    console.log(text);
    ...
    
  3. htmlEscape()
    String.prototype.htmlEscape = function (){
      var escapedStr = String(this).replace(/&/g, '&amp;');
      escapedStr = escapedStr.replace(/</g, '&lt;');
      escapedStr = escapedStr.replace(/>/g, '&gt;');
      escapedStr = escapedStr.replace(/"/g, '&quot;');
      escapedStr = escapedStr.replace(/'/g, "&#39");
      return escapedStr;
    
  4. htmlEscape()
    String.prototype.htmlEscape = function() {
        return this.replace(/&/g, '&amp;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;');
    console.log('<script>'.htmlEscape()); 
    
  5. htmlEscape()
    "use strict";
    String.prototype.htmlEscape = function()
      return this.
        replace(/&/g, '&amp;').
        replace(/</g, '&lt;').
        replace(/>/g, '&gt;').
        replace(/"/g, '&quot;').
        replace(/\n/g, '<br/>');
    ...
    
  6. htmlEscape()
    String.prototype.htmlEscape = function () {
      return this.replace(/"/g, '&quot;').replace(/>/g, '&gt;').replace(/</g, '&lt;').replace(/&/g, '&amp;');
    };
    
  7. normalizeHtml()
    String.prototype.normalizeHtml = function () {
        return  this.replace(/(\n|\r|\t|\s\s+)/gm, "").trim();
    };