前端开发
325
使用正则去掉富文本编辑器的THML标签和换行回车等标记
实际中的例子:
代码实现:
react中js的写法:this.fiterLabelHandle(this.state.majorInfoList.schemeIntroduce); // 调用过滤掉没用的格式或字符的方法 其中this.state.majorInfoList.schemeIntroduce的格式如: <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> // 过滤掉富文本里面包含的html标签和空格符等东西的方法 fiterLabelHandle = (schemeIntroduce) =>{ schemeIntroduce = schemeIntroduce.replace(/(\n)/g, ""); // 去掉换行 schemeIntroduce = schemeIntroduce.replace(/(\t)/g, ""); // 去掉换行 schemeIntroduce = schemeIntroduce.replace(/(\r)/g, ""); schemeIntroduce = schemeIntroduce.replace(/<\/?[^>]*>/g, ""); // 去掉标签 schemeIntroduce = schemeIntroduce.replace(/\s*/g, ""); schemeIntroduce = schemeIntroduce.replace(/ /ig, " "); // 去掉 this.setState({ schemeIntroduce : schemeIntroduce }); }