使用正则去掉富文本编辑器的THML标签和换行回车等标记
实际中的例子:
代码实现:
1react中js的写法:this.fiterLabelHandle(this.state.majorInfoList.schemeIntroduce); // 调用过滤掉没用的格式或字符的方法 2 3其中this.state.majorInfoList.schemeIntroduce的格式如: 4 5<p><strong><span style="font-family:等线;font-weight:normal">测试账号 信息学院:</span></strong><span style="font-size:13px;font-family:'Segoe UI',sans-serif;background:#D5EEFF">20081077 </span><span style="font-size:13px;background:#D5EEFF">外国语学院:</span><span style="font-size: 13px;font-family:'Segoe UI',sans-serif;background:white">20051012</span><span style="font-size:13px;font-family:'Segoe UI',sans-serif;background:#D5EEFF"> admin</span></p><h1><strong><span style="font-family:等线">1<span style="font:9px 'Times New Roman'"> </span></span></strong><strong><span style="font-family:等线">前端优化</span></strong></h1><h1><strong><span style="font-family:等线">2<span style="font:9px 'Times New Roman'"> </span></span></strong><strong><span style="font-family:等线">系统级别</span></strong></h1><h1><strong><span style="font-family:等线">3<span style="font:9px 'Times New Roman'"> </span></span></strong>方案版本管理</h1><h2><strong><span style="font-family:'等线 Light'">3.1<span style="font:9px 'Times New Roman'"></span></span></strong><strong><span style="font-family:等线">版本信息管理</span></strong></h2><h3>3.1.1<span style="font:9px 'Times New Roman'"> </span>主页面</h3><p><br/></p> 6 7// 过滤掉富文本里面包含的html标签和空格符等东西的方法 8 fiterLabelHandle = (schemeIntroduce) =>{ 9 schemeIntroduce = schemeIntroduce.replace(/(\n)/g, ""); // 去掉换行 10 schemeIntroduce = schemeIntroduce.replace(/(\t)/g, ""); // 去掉换行 11 schemeIntroduce = schemeIntroduce.replace(/(\r)/g, ""); 12 schemeIntroduce = schemeIntroduce.replace(/<\/?[^>]*>/g, ""); // 去掉标签 13 schemeIntroduce = schemeIntroduce.replace(/\s*/g, ""); 14 schemeIntroduce = schemeIntroduce.replace(/ /ig, " "); // 去掉 15 this.setState({ 16 schemeIntroduce : schemeIntroduce 17 }); 18 }