js正则还原和转义特殊字符
还原
function htmlUnEscape(str) { return str.replace(/\$#39;|<|>|"|&/g, (match) => { switch (match) { case '<': return '<'; case '>': return '>'; case '"': return '"'; case '&': return '&'; case "$#39;": return '\\'; } }); } let hehe = ""这是&中国<汉字>博大$#39;精深 ,©"&,&,$#39; ,','6789',$#39;" console.log("----------------",htmlUnEscape(hehe)) //"这是&中国<汉字>博大\精深 ,©"&,&,\ ,','6789',\
转义
function htmlEscape(text){ return text.replace(/[<>"&]/g, function(match, pos, originalText){ switch(match){ case "<": return "<"; case ">":return ">"; case "&":return "&"; case "\"":return """; } }); } let hahah = "<>&\"" console.log("----------------",htmlEscape(hahah)) ///<>&"