Nodejs HTML Unescape unescapeHtml()

Here you can find the source of unescapeHtml()

Method Source Code

/**//from w w  w.j a v  a  2 s.  c om
 * Extended Objects
 *
 * Adds additional methods to javascript built-in types
 */

// Function for unescaping HTML encoded characters
String.prototype.unescapeHtml = function () {
    
    // Create a fake element and assign our string to its contents
    var temp = document.createElement("div");
    temp.innerHTML = this;
    
    // Get the contents (now natively escaped), remove the child node and retuin
    var result = temp.childNodes.length === 0 ? "" : temp.childNodes[0].nodeValue;
    if(temp.firstChild) temp.removeChild(temp.firstChild);
    return result;
};

Related

  1. unescape()
    String.prototype.unescape = function() {
      return unescape(this).replace(/[-_]/g, ' ');
    
  2. unescapeEntities()
    String.prototype.unescapeEntities = function() {
      return this.replace(/&#([0-9]{1,4});/gi, function(match, numStr) {
        var num = parseInt(numStr, 10);
        return String.fromCharCode(num);
      });
    };
    
  3. unescapeHTML()
    String.prototype.unescapeHTML = function() {
      return String(this).replace("&amp;", "&").replace("&lt;", "<").replace("&gt;", ">").replace('&quot;', '"').replace('&#39;', "'").replace('&#x2F;', "/");
    
  4. unescapeHtml()
    String.prototype.unescapeHtml = function () {
        var temp = document.createElement("div");
        temp.innerHTML = this;
        var result = temp.childNodes.length === 0 ? "" : temp.childNodes[0].nodeValue;
        if(temp.firstChild) temp.removeChild(temp.firstChild);
        return result;
    };
    
  5. unescapeHtml()
    String.prototype.unescapeHtml = function () {
      var temp = document.createElement("div");
      temp.innerHTML = this;
      var result = "";
      for (var i = 0; i < temp.childNodes.length; i++) {
       result = result + temp.childNodes[i].nodeValue;
      temp.removeChild(temp.firstChild)
      return result;
    ...
    
  6. unescapeHtml()
    String.prototype.unescapeHtml = function () {
        var temp = document.createElement("div");
        temp.innerHTML = this;
        var result = temp.childNodes[0].nodeValue;
        temp.removeChild(temp.firstChild);
        return result;
    
  7. unescapeHtmlEntities()
    String.prototype.unescapeHtmlEntities = function () {
      try{
        var temp = document.createElement('textarea');
        temp.innerHTML = this;
        return temp.value;
      }catch(e){
        return this;
    
  8. unescapeHtmlEntities()
    String.prototype.unescapeHtmlEntities = function () {
      try{
        var temp = document.createElement('textarea');
        temp.innerHTML = this;
        return temp.value;
      }catch(e){
        return this;