Javascript String decodeHTML()

Description

Javascript String decodeHTML()



String.prototype.decodeHTML = function() {
 var map = {//from  www. j  ava 2 s  . c  o m
  "gt":">",
  "lt":"<",
  "quot":"\""
 };
 return this.replace(/&(#(?:x[0-9a-f]+|\d+)|[a-z]+);?/gi, function($0, $1) {
  if ($1[0] === "#") {
   return String.fromCharCode($1[1].toLowerCase() === "x" ? parseInt($1.substr(2), 16)  : parseInt($1.substr(1), 10));
  } else {
   return map.hasOwnProperty($1) ? map[$1] : $0;
  }
 });
};

Javascript String decodeHtml()

// decode an html encoded string
String.prototype.decodeHtml = function () {
    var string = new String(this);
    //from ww w. j  a v  a  2s .  co  m
    string = string.replace(/&lt;/g, "<");
    string = string.replace(/&gt;/g, ">");
    string = string.replace(/&quot;/g,  "\"")
    string = string.replace(/&39;/g,  "'");
    string = string.replace(/&amp;/g, "&");
    
    return string
}



PreviousNext

Related