gpt4 book ai didi

js处理网页编辑器转义、去除转义、去除HTML标签的正则

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 30 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章js处理网页编辑器转义、去除转义、去除HTML标签的正则由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

富文本编辑器生成的HTML标签,进行转义,然后写入数据库,防止脚本注入:

?
1
2
3
function htmlEncode(value){
   return $( '<div/>' ).text(value).html();
}

1、从数据库拿出的转义后的HTML标签内容,先得去除转义,然后再去除HTML标签,是生成缩略文字.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
/*移除HTML标签代码*/
  function removeHTMLTag(str) {
    str = str.replace(/<\/?[^>]*>/g, '' ); //去除HTML tag
    str = str.replace(/[ | ]*\n/g, '\n' ); //去除行尾空白
    //str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
    str=str.replace(/ /ig, '' ); //去掉
    return str;
  }
  //转意符换成普通字符
  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];});
  }

如果是文章详情页的话,直接去除转义就可以显示在页面了:

?
1
2
3
4
5
//转意符换成普通字符
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+;?/g,''),

3、js处理去掉富文本编辑的html,样式,只显示纯文字内容,以供列表页使用 。

?
1
2
3
4
5
6
7
8
9
<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 id="codetool">

4、jQuery JavaScript正则表达式与\n代替<BR> 。

Lee TaylorTeneff给出了该问题的处理方式:

var str = document.getElementById('mydiv').innerHTML; document.getElementById('mytextarea').innerHTML = str.replace(/<br\s*[\/]?>/gi, "\n"),

or using jQuery

var str = $("#mydiv").html(); var regex = /<br\s*[\/]?>/gi; $("#mydiv").html(str.replace(regex, "\n")),

5、要去除html标签,图片,换行,回车等 。

?
1
2
3
4
5
description = description.replace(/(\n)/g, "" );
description = description.replace(/(\t)/g, "" );
description = description.replace(/(\r)/g, "" );
description = description.replace(/<\/?[^>]*>/g, "" );
description = description.replace(/\s*/g, "" );

6、我小编写的一个替换fackeditor中的多余br与空行的 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//加强替换主要是考虑多个br的问题
function doRepAdvance(s){
var str=s.replace(/<p><br type= "_moz" ><\/p>/ig, "" );
str=str.replace(/<br type= "_moz" >\n&nbsp;<\/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&nbsp;<\/p>/ig, "</p>" );
str=str.replace(/<br>\n&nbsp;<\/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>&nbsp;<\/p>/ig, "" );
str=str.replace(/<p> <\/p>/ig, "" );
str=str.replace(/<p><\/p>/ig, "" );
return str;
}

到此这篇关于js处理富文本编辑器转义、去除转义、去除HTML标签的正则的文章就介绍到这了,更多相关编辑器转义、去除转义、去除HTML标签内容请搜素我以前的文章或下面相关文章,希望大家以后多多支持我! 。

  • 正则
  • JS
  • html标签
  • 转义
  • 网页编辑器
  • 延伸 · 阅读

    • 2020-09-06js通过正则匹配没有内容的空标签
    • 2020-09-05解决正则表示式匹配($regex)引起的一次mongo数据库
    • 2020-09-05Java正则相关的Pattern和Matcher类及遇到的坑
    • 2020-09-05正则爬取京东商品信息并打包成.exe可执行程序
    • 2020-09-05JS正则表达式必须包含数字、字母、特殊字符
    • 2020-09-04Python使用正则表达式去除(过滤)HTML标签提取文字
    精彩推荐
    • 正则表达式正则表达式{n,m}量词(至少n次,最多m次)

      正则表达式{n,m}量词(至少n次,最多m次)

      这篇文章主要介绍了正则表达式{n,m}量词,可以重复前面匹配的字符n-m次,至少n次,最多m次,需要的朋友可以参考下... 。

      正则教程网 424 2020-08-25
    • 正则表达式利用正则快速找出两个字符串的不同字符

      利用正则快速找出两个字符串的不同字符

      本文主要讲解利用正则快速找出两个字符串的不同字符的实现方法,有需要的同学可以参考下... 。

      正则之家 309 2020-07-16
    • 正则表达式JS 正则表达式的位置匹配

      JS 正则表达式的位置匹配

      JS中正则表达式的位置匹配代码,国外翻译的文章。... 。

      正则之家 296 2020-07-28
    • 正则表达式php 正则表达式小结

      php 正则表达式小结

      正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串、将匹配的子串做替换或者从某个串中取出符合某个条件... 。

      正则之家 462 2020-07-28
    • 正则表达式文本框输入限制的正则表达式大全

      文本框输入限制的正则表达式大全

      一些常用的文本框输入限制的正则表达式分享给大家,有需要的可以参考下... 。

      正则之家 423 2020-07-07
    • 正则表达式JS正则表达式提取字符串中所有汉字的脚本

      JS正则表达式提取字符串中所有汉字的脚本

      在网上发现有人用vbscript正则表达式实现了这个功能,但代码很厂,偶改成js的了,很短的一段代码... 。

      正则之家 362 2020-07-18
    • 正则表达式积累比较常用的正则表达式(例如:匹配中文、匹配html)

      积累比较常用的正则表达式(例如:匹配中文、匹配html)

      本文是小编在日常工作中积累并整理的有关一些常用的正则表达式(例如:匹配中文、匹配html),在此把全部内容分享在脚本之家网站,需要的朋友可以来脚... 。

      正则之家 371 2020-08-17
    • 正则表达式正则表达式之文本模式的匹配和查找

      正则表达式之文本模式的匹配和查找

      这篇文章主要介绍了正则表达式之文本模式的匹配和查找操作方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下... 。

      IT派森 125 2020-09-05

    最后此篇关于js处理网页编辑器转义、去除转义、去除HTML标签的正则的文章就讲到这里了,如果你想了解更多关于js处理网页编辑器转义、去除转义、去除HTML标签的正则的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com