• 后台构建 html 字符串传到前台字符串转码(html)处理


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

    例如后台 html 字符串是

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

    转码

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

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

    复制代码
     function decodeHtml(s) {
            var HTML_DECODE = {
                "&lt;": "<",
                "&gt;": ">",
                "&amp;": "&",
                "&nbsp;": " ",
                "&quot;": """,
                "&copy;": ""
    
            <span style="color: #008000;">//</span><span style="color: #008000;"> Add more</span>
    

    };

        </span><span style="color: #0000ff;">var</span> REGX_HTML_ENCODE = /"|&amp;|'|&lt;|&gt;|[x00-x20]|[x7F-xFF]|[u0100-u2700]/<span style="color: #000000;">g;
    
        </span><span style="color: #0000ff;">var</span> REGX_HTML_DECODE = /&amp;w+;|&amp;#(d+);/<span style="color: #000000;">g;
    
        </span><span style="color: #0000ff;">var</span> REGX_TRIM = /(^s*)|(s*$)/<span style="color: #000000;">g;
    
        s </span>= (s != undefined) ? s : ""<span style="color: #000000;">;
        </span><span style="color: #0000ff;">return</span> (<span style="color: #0000ff;">typeof</span> s != "string") ?<span style="color: #000000;"> s :
            s.replace(REGX_HTML_DECODE,
                      </span><span style="color: #0000ff;">function</span> ($0, $1<span style="color: #000000;">) {
                          </span><span style="color: #0000ff;">var</span> c = HTML_DECODE[$0<span style="color: #000000;">];
                          </span><span style="color: #0000ff;">if</span> (c ==<span style="color: #000000;"> undefined) {
                              </span><span style="color: #008000;">//</span><span style="color: #008000;"> Maybe is Entity Number</span>
                              <span style="color: #0000ff;">if</span> (!isNaN($1<span style="color: #000000;">)) {
                                  c </span>= String.fromCharCode(($1 == 160) ? 32 : $1<span style="color: #000000;">);
                              } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                                  c </span>= $0<span style="color: #000000;">;
                              }
                          }
                          </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> c;
                      });
    };</span></pre>
    
    复制代码
  • 相关阅读:
    讲讲HashCode的作用
    自定义一个类加载器
    从为什么String=String谈到StringBuilder和StringBuffer
    System.load(String filename)和System.loadLibrary(String libname)的区别
    自己实现一个Native方法的调用
    Java对象表示方式1:序列化、反序列化和transient关键字的作用
    Java对象表示方式2:XStream实现对对象的XML化
    static关键字作用
    final的作用
    多态
  • 原文地址:https://www.cnblogs.com/jpfss/p/9546312.html
Copyright © 2020-2023  润新知