欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

后台构建 html 字符串传到前台字符串转码(html)处理_html/css_WEB-ITnose

程序员文章站 2022-05-13 23:07:17
...
知识在于总结,那就记下了吧!

例如后台 html 字符串是

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

转码

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

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

 function decodeHtml(s) {        var HTML_DECODE = {            "<": "",            "&": "&",            " ": " ",            """: "\"",            "©": ""            // 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;                      });    };