美文网首页
js转义与反转义

js转义与反转义

作者: 九乡河的小香瓜 | 来源:发表于2019-08-21 23:53 被阅读0次

处理前台输入与后台返回时要注意转义与反转义.
转义:

function    htmlEncodeByRegExp (str){  
        str=str.toString();
        var temp = "";
        if(str.length == 0) return "";
        temp = str.replace(/&/g,"&");
        temp = temp.replace(/</g,"&lt;");
        temp = temp.replace(/>/g,"&gt;");
        temp = temp.replace(/\s/g,"&nbsp;");
        temp = temp.replace(/\'/g,"&#39;");
        temp = temp.replace(/\"/g,"&quot;");
        console.log(temp);
        return temp;
    }

反转义

function htmlDecodeByRegExp(str){  
        str=str.toString();
        var temp = "";
        if(str.length == 0) return "";
        temp = str.replace(/&amp;/g,"&");
        temp = temp.replace(/&lt;/g,"<");
        temp = temp.replace(/&gt;/g,">");
        temp = temp.replace(/&nbsp;/g," ");
        temp = temp.replace(/&#39;/g,"\'");
        temp = temp.replace(/&quot;/g,"\"");
        return temp;  
    }

相关文章

  • markdown 转义反引号

    普通反斜杠无法转义反引号正确转义 markdown文本 显示文本这是`反引号

  • js转义与反转义

    处理前台输入与后台返回时要注意转义与反转义.转义: 反转义

  • python学习笔记02-字符串+表达式+运算符

    # 字符串 - 转义 - 格式化 - 内置函数 ## 转义 - \ 反斜杠转义(同php) eg:\' \\ \n...

  • 转义字符与原字符

    一、 什么是转义字符? 答:转义字符就是反斜杠+想要实现的转义功能首字母。 二、 为什么需要转义字符?如何使用? ...

  • Python语法小记忆

    1、转义字符 Python 中的字符串还支持转义字符。所谓转义字符是指使用反斜杠“\”对一些特殊字符进行转义。 转...

  • Markdown基础语法

    忽略语法 加\转义 (eg: \*=*)可以转义的有: \ 反斜杠` 反引号* 星号_ 下划线{} 大括号[...

  • Python中的字符串处理

    Python转义字符 在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符。如下表: 转义字符描述 \...

  • Python 基础2

    Python 转义字符 在需要在字符中使用特殊字符时,python 用反斜杠\转义字符。如下表: 转义字符描述 \...

  • Python字符串和变量

    字符串 转义字符 格式化 转义字符 用回车键,换行键,退格键 借助反斜杠字符,进行转义 在字符串中,一旦出现反斜杠...

  • 反斜杠

    Windows把反斜杠(\) 作为目录分隔符,而python用反斜杠表示转义符! 所以,你得使用转义符来表示反斜杠...

网友评论

      本文标题:js转义与反转义

      本文链接:https://www.haomeiwen.com/subject/cpzwsctx.html