使用正則去掉富文本編輯器的THML標籤和換行回車等標記javascript
實際中的例子:html
代碼實現:前端
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 }); }