• js实现转义、反转义


    两种思路,一种是列出需要用到的转义项,通过正则来转化;另一种通过转化为html语言,通过浏览器帮助我们翻译,然后获取innerText

    var HtmlUtil = {
            /*1.用浏览器内部转换器实现html编码(转义)*/
            htmlEncode:function (html){
                //1.首先动态创建一个容器标签元素,如DIV
                var temp = document.createElement ("div");
                //2.然后将要转换的字符串设置为这个元素的innerText或者textContent
                (temp.textContent != undefined ) ? (temp.textContent = html) : (temp.innerText = html);
                //3.最后返回这个元素的innerHTML,即得到经过HTML编码转换的字符串了
                var output = temp.innerHTML;
                temp = null;
                return output;
            },
            /*2.用浏览器内部转换器实现html解码(反转义)*/
            htmlDecode:function (text){
                //1.首先动态创建一个容器标签元素,如DIV
                var temp = document.createElement("div");
                //2.然后将要转换的字符串设置为这个元素的innerHTML(ie,火狐,google都支持)
                temp.innerHTML = text;
                //3.最后返回这个元素的innerText或者textContent,即得到经过HTML解码的字符串了。
                var output = temp.innerText || temp.textContent;
                temp = null;
                return output;
            },
            /*3.用正则表达式实现html编码(转义)*/
            htmlEncodeByRegExp:function (str){
                 var temp = "";
                 if(str.length == 0) return "";
                 temp = str.replace(/&/g,"&");
                 temp = temp.replace(/</g,"<");
                 temp = temp.replace(/>/g,">");
                 temp = temp.replace(/\s/g," ");
                 temp = temp.replace(/\'/g,"'");
                 temp = temp.replace(/\"/g,""");
                 return temp;
            },
            /*4.用正则表达式实现html解码(反转义)*/
            htmlDecodeByRegExp:function (str){
                 var temp = "";
                 if(str.length == 0) return "";
                 temp = str.replace(/&/g,"&");
                 temp = temp.replace(/</g,"<");
                 temp = temp.replace(/>/g,">");
                 temp = temp.replace(/ /g," ");
                 temp = temp.replace(/'/g,"\'");
                 temp = temp.replace(/"/g,"\"");
                 return temp;
            },
            /*5.用正则表达式实现html编码(转义)(另一种写法)*/
            html2Escape:function(sHtml) {
                 return sHtml.replace(/[<>&"]/g,function(c){return {'<':'<','>':'>','&':'&','"':'"'}[c];});
            },
            /*6.用正则表达式实现html解码(反转义)(另一种写法)*/
            escape2Html:function (str) {
                 var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'};
                 return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];});
            }
        };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
  • 相关阅读:
    maven学习: 使用Maven构建Web项目
    Spring常见问题解决 - @WebFilter 过滤器使用@Order控制执行顺序失效了?
    机器学习实战(4)——训练模型
    八月 NFT 行业解读:数据下滑,熊市持续
    GitHub爆赞的RocketMQ分布式中间件学习手册,竟一夜下载量破10W+
    布隆过滤器
    基于生成对抗网络探索潜在空间的医学图像融合算法
    【综述】推荐系统偏差问题 & 去偏最新研究进展(Bias and Debias in Recommender System)
    nginx的优化和防盗链
    安装element-plus
  • 原文地址:https://blog.csdn.net/q879936814/article/details/136395265