js处理网页编辑器转义、去除转义、去除HTML标签的正则
(编辑:jimmy 日期: 2024/11/18 浏览:3 次 )
富文本编辑器生成的HTML标签,进行转义,然后写入数据库,防止脚本注入:
function htmlEncode(value){ return $('<div/>').text(value).html(); }
1、从数据库拿出的转义后的HTML标签内容,先得去除转义,然后再去除HTML标签,是生成缩略文字。
/*移除HTML标签代码*/ function removeHTMLTag(str) { str = str.replace(/<\/"'}; return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];}); }
如果是文章详情页的话,直接去除转义就可以显示在页面了:
//转意符换成普通字符 function escape2Html(str) { var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'}; return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];}); }
2、JS正则过滤(去除)富文本编辑器中的FONT-SIZE标签
var test=test.replace(/font-size:\w+;"htmlcode">
<script type="text/javascript"> var description = '<p style="margin-top:19.5pt;margin-right:0cm;margin-bottom:19.5pt;margin-left: 0cm;text-indent:24.1pt;mso-char-indent-count:2.0;mso-pagination:widow-orphan"> <b><span lang="EN-US" style="font-family:宋体;mso-bidi-font-family:宋体;color:#252525">1.</span></b><b><span style="font-family:宋体;mso-bidi-font-family:宋体;color:#252525">国际保险经纪行业收入分析<span lang="EN-US"><o:p></o:p></span></span></b></p> <p style="margin-top:19.5pt;margin-right:0cm;margin-bottom:19.5pt;margin-left: 0cm;text-indent:24.0pt;mso-char-indent-count:2.0;mso-pagination:widow-orphan"> <span lang="EN-US" style="font-family:宋体;mso-bidi-font-family:宋体;color:#252525">2010</span><span style="font-family:宋体;mso-bidi-font-family:宋体;color:#252525">年全球保险经纪行业市场规模为<span lang="EN-US">437.56</span>亿美元,<span lang="EN-US">2015</span>年增长至<span lang="EN-US">581.3</span>亿美元。<span lang="EN-US"><o:p></o:p></span></span></p> <p align="center" style="margin-top:19.5pt;margin-right:0cm;margin-bottom:19.5pt; margin-left:0cm;text-align:center;mso-pagination:widow-orphan"> <span lang="EN-US" style="font-family:宋体;mso-bidi-font-family:宋体;color:#252525">2010-2015</span><span style="font-family:宋体;mso-bidi-font-family:宋体;color:#252525">年国际保险经纪行业市场规模:亿美元</span></p> <p style="margin: 19.5pt 0cm; text-indent: 24.1pt; text-align: center;"> <img alt="" src="/UploadFiles/2021-04-02/11.png">4、jQuery JavaScript正则表达式与\n代替<BR>
Lee TaylorTeneff给出了该问题的处理方式:
var str = document.getElementById('mydiv').innerHTML;
document.getElementById('mytextarea').innerHTML = str.replace(/<br\s*[\/]"\n");or using jQuery:
var str = $("#mydiv").html();
var regex = /<br\s*[\/]"#mydiv").html(str.replace(regex, "\n"));5、要去除html标签,图片,换行,回车等
description = description.replace(/(\n)/g, ""); description = description.replace(/(\t)/g, ""); description = description.replace(/(\r)/g, ""); description = description.replace(/<\/""); description = description.replace(/\s*/g, "");6、小编写的一个替换fackeditor中的多余br与空行的
//加强替换主要是考虑多个br的问题 function doRepAdvance(s){ var str=s.replace(/<p><br type="_moz"><\/p>/ig,""); str=str.replace(/<br type="_moz">\n <\/p>/ig, "</p>"); str=str.replace(/<br type="_moz">\n<\/p>/ig, "</p>"); str=str.replace(/<br type="_moz">\n\r<\/p>/ig, "</p>"); str=str.replace(/<br type="_moz"><\/p>/ig,"</p>"); str=str.replace(/<br \/>\n <\/p>/ig, "</p>"); str=str.replace(/<br>\n <\/p>/ig, "</p>"); str=str.replace(/<br \/>\n<\/p>/ig, "</p>"); str=str.replace(/<br \/>\n\r<\/p>/ig, "</p>"); str=str.replace(/(<br>\n){1,}<\/p>/ig,"</p>"); str=str.replace(/(<br>){1,}<\/p>/ig,"</p>"); str=str.replace(/<br>\n<\/p>/ig, "</p>"); str=str.replace(/<br><\/p>/ig,"</p>"); str=str.replace(/<p> <\/p>/ig,""); str=str.replace(/<p> <\/p>/ig,""); str=str.replace(/<p> <\/p>/ig,""); str=str.replace(/<p><\/p>/ig,""); return str; }
下一篇:使用正则表达式生成随机数据的方法