非常简单的 html 转义与反转义
//把HTML格式的字符串转义成实体格式字符串
function escapeHTMLString(str) {
str = str.replace(/</g,'<');
str = str.replace(/>/g,'>');
return str;
}
//把实体格式字符串转义成HTML格式的字符串
function escapeStringHTML(str) {
str = str.replace(/</g,'<');
str = str.replace(/>/g,'>');
return str;
}
var str = '<script>alert("abc");</script>';
var str1 = escapeHTMLString(str);
console.log(str1); // '<script>alert("abc");</script>'
var str2 = escapeStringHTML(str1);
console.log(str2); //'<script>alert("abc");</script>'