富文本转义问题

2,057 阅读1分钟

出现原因: 后台管理系统使用了富文本编辑器进行编辑时候,用户竟然输入带html标签的文章内容或者被后台解析转义了特定标签,导致前台显示的时候得到都内容为如下图所示:

image.png 所以会导致显示出来的就是html标签。

解决:

/**
解决内容被转义问题
 */
function htmlDecodeByRegExp(str) {
    var temp = "";
    if (str.length == 0) return "";
    temp = str.replace(/&/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, '"');
    temp = temp.replace(/↵/g, "");
    return temp;
}