前端获取的内容通过转义封装成json格式传到后台
function repCom(str){
var str_r = (str+"").replace(/\r\n/g,"$lt;br/$gt;");
str_r = str_r.replace(/\r/g,"$lt;br")
str_r = str_r.replace(/\n/g,"$lt/$gt;");
str_r = str_r.replace(/'/g,"$apos;");
str_r = str_r.replace(/"/g,"$quot;");
str_r = str_r.replace(/&/g,"$and;");
str_r = str_r.replace(/%/g,"$per$son;");
str_r = str_r.replace(/#/g,"$jing;");
str_r = str_r.replace(/\\/g,"$gang;");
str_r = str_r.replace(/{/g,"$leftbracket;");
str_r = str_r.replace(/}/g,"$rightbracket;");
str_r = str_r.replace(/:/g,"$colonRep;");
str_r = str_r.replace(/\+/g,"$jhth;");
return str_r;
}
转义字符:
显示结果 | 描述 | 实体名称 |
---|---|---|
空格 |   | |
< | 小于号 | < |
> | 大于号 | > |
& | 和号 | & |
’ | 撇号 | &apos |
" | 引号 | $quot |
后台获取到解析的json数据再反着转义回来,存储到数据库
// 接收前端值通用模式
public static String repCom(String str) {
String str_replace = str.replace("$lt;br/$gt;", "\r\n");
str_replace = str_replace.replace("$lt;br", "\r\n");
str_replace = str_replace.replace("$lt/$gt;", "\r\n");
str_replace = str_replace.replace("$apos;", "''");
str_replace = str_replace.replace("$quot;", "\"");
str_replace = str_replace.replace("$and;", "&");
str_replace = str_replace.replace("$per$son;", "%");
str_replace = str_replace.replace("$jing;", "#");
str_replace = str_replace.replace("$gang;", "\\\\");
str_replace = str_replace.replace("$leftbracket;", "{");
str_replace = str_replace.replace("$rightbracket;", "}");
str_replace = str_replace.replace("$colonRep;", ":");
str_replace = str_replace.replace("$jhth;", "+");
return str_replace;
}