知识在于总结,那就记下了吧!

 

例如后台 html 字符串是

var htmlStr="后台html字符串";

转码

var html格式代码=decodeHtml(htmlStr);

只需调用下面这个js方法就行了

 function decodeHtml(s) {
        var HTML_DECODE = {
            "&lt;": "<",
            "&gt;": ">",
            "&amp;": "&",
            "&nbsp;": " ",
            "&quot;": "\"",
            "&copy;": ""

            // Add more
        };

        var REGX_HTML_ENCODE = /"|&|'|<|>|[\x00-\x20]|[\x7F-\xFF]|[\u0100-\u2700]/g;

        var REGX_HTML_DECODE = /&\w+;|&#(\d+);/g;

        var REGX_TRIM = /(^\s*)|(\s*$)/g;

        s = (s != undefined) ? s : "";
        return (typeof s != "string") ? s :
            s.replace(REGX_HTML_DECODE,
                      function ($0, $1) {
                          var c = HTML_DECODE[$0];
                          if (c == undefined) {
                              // Maybe is Entity Number
                              if (!isNaN($1)) {
                                  c = String.fromCharCode(($1 == 160) ? 32 : $1);
                              } else {
                                  c = $0;
                              }
                          }
                          return c;
                      });
    };
View Code

 

posted on 2015-05-29 13:55  三分钟~\乐度  阅读(5743)  评论(1编辑  收藏  举报