前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >javascript转义unicode十六进制编码且带有反斜杠后的html

javascript转义unicode十六进制编码且带有反斜杠后的html

作者头像
蓓蕾心晴
发布2018-12-28 16:27:41
1.7K0
发布2018-12-28 16:27:41
举报
文章被收录于专栏:前端小叙前端小叙
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <style>
        .container{
            padding:30px 60px;
        }
        .wrap{
            padding:10px 0;
        }
        .textarea{
            width: 98%;
            border: 1px solid #dcdee2;
            height: 320px;
            resize: vertical;
            text-shadow: 0 1px 0 rgba(255,255,255,.5);
            -webkit-border-radius: 4px;
            -moz-border-radius: 4px;
            border-radius: 4px;
            padding: 5px 10px;
            margin-bottom:10px;
        }
        .btn{
            display: inline-block;
            margin-bottom: 0;
            font-weight: 400;
            text-align: center;
            -ms-touch-action: manipulation;
            touch-action: manipulation;
            cursor: pointer;
            background-image: none;
            border: 1px solid transparent;
            white-space: nowrap;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
            padding: 5px 15px 6px;
            font-size: 12px;
            border-radius: 4px;
            transition: color .2s linear,background-color .2s linear,border .2s linear,box-shadow .2s linear;
            color: #515a6e;
            background-color: #fff;
            border-color: #dcdee2;
        }
        .btn-primary {
            color: #fff;
            background-color: #2d8cf0;
            border-color: #2d8cf0;
            font-size: 15px;
            margin-bottom:20px;
        }
        .btn-primary:hover {
            color: #fff;
            background-color: #57a3f3;
            border-color: #57a3f3;
        }
       .btn:focus {
            outline: 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>在线转义html代码</h1>
        <p>粘贴代码(unicode十六进制代码)</p>
        <div>
            <textarea name="" class="textarea" id="response-html" placeholder="粘贴response填充代码,形如:\u003c!doctype html\u003e \u003chtml\u003e \u003chead\u003e \u003ctitle\u003eMintegral Interactive Ad\u003c/title\u003e \u003clink href=https://cdn.bootcss.com/Swiper/4.3.2/css/swiper.min.css rel=stylesheet\u003e \u003cmeta name=viewport content=\"initial-scale=1,maximum-scale=1,user-scalable=no\"/\u003e \u003cmeta name=author content=Mintegral\u003e \u003cmeta http-equiv=Content-Type content=\"text/html; charset=utf-8\"\u003e \u003cstyle\u003eg/1.jpg)\u003e \u003cdiv class=obg\u003e\u003c/div\u003e \u003ca …………"></textarea>
            <button class="btn btn-primary" type="button" id="btn-transfer">转义</button>
            <textarea name="" class="textarea" id="transfer-html"></textarea>
        </div>
    </div>
    <script>
        //转义html代码
        function decodeUnicode(str) {
            //先把十六进制unicode编码/u替换为%u
            str = str.replace(/\\u/gi,'%u');
            //再把页面中反斜杠替换为空
            str = str.replace(/\\/gi,'');
            return unescape(str);
        }
        let btn=document.getElementById("btn-transfer");
        let responseHtml=document.getElementById("response-html");
        let transferHtml=document.getElementById("transfer-html");
        btn.onclick=function () {
            //获取当前转义前html
            let html=responseHtml.value;
            //输出转义后html
            transferHtml.value=decodeUnicode(html);
        }
    </script>
</body>
</html>

由于后端返回的html代码中所有标签前后都有反斜杠“\”,且有\uxxxx形式的十六进制unicode编码,如果直接把所有反斜杠替换为%,则会把标签前后的反斜杠一并替换,导致最后无法转义,所以先把十六进制开头的\u替换为%u,则可以使用unescape转码,然后再单独把反斜杠替换为空返回即可。这里使用decodeURI或者decodeURIComponent方法会报错,应该代码格式不对。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-12-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档