1、Js去标签,使用正则表达式
let text = this.jeditor.value
replace = text.replace(/<[^>]+>/g,"");
replace1= replace.replace(/ /ig, '')
2、Java去标签,使用replaceAll()和正则表达式
public class DelTagsUtil {
public static String delHtmlTags(String htmlStr) {
String scriptRegex="<script[^>]*?>[\\s\\S]*?<\\/script>";
String styleRegex="<style[^>]*?>[\\s\\S]*?<\\/style>";
String htmlRegex="<[^>]+>";
String spaceRegex = "\\s*|\t|\r|\n";
htmlStr = htmlStr.replaceAll(scriptRegex, "");
htmlStr = htmlStr.replaceAll(styleRegex, "");
htmlStr = htmlStr.replaceAll(htmlRegex, "");
htmlStr = htmlStr.replaceAll(spaceRegex, "");
return htmlStr.trim();
}
public static String getTextFromHtml(String htmlStr){
htmlStr = delHtmlTags(htmlStr);
htmlStr = htmlStr.replaceAll(" ","");
return htmlStr;
}
public static void main(String[] args){
String htmlStr= "<p><span style="color: #333333; font-family: 'Microsoft YaHei', SimHei; font-size: 13.91px; background-color: #ffffff;">先帝创业未半而中道崩殂,今天下三分,</span><span style="color: #333333; font-family: 'Microsoft YaHei', SimHei; font-size: 13.91px; background-color: #ffffff;">益州疲弊,此诚危急存亡之秋也。</span></p> <p><span style="color: #333333; font-family: 'Microsoft YaHei', SimHei; font-size: 13.91px; background-color: #ffffff;">然侍卫之臣不懈于内,</span><span style="color: #333333; font-family: 'Microsoft YaHei', SimHei; font-size: 13.91px; background-color: #ffffff;">忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。</span></p> <p><span style="color: #333333; font-family: 'Microsoft YaHei', SimHei; font-size: 13.91px; background-color: #ffffff;">诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。</span></p>";
System.out.println(getTextFromHtml(htmlStr));
}
}
输出结果:
先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。然侍卫之臣不懈于内, 忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。