java開發中jsp頁面可以嵌套很多插件就可以將html形式的文本直接轉化為純文本,但是如果你已經保存下來或者沒有運用插件,這個額html形式的文本你該怎么轉化為純文本呢?有次我將公告保存了html形式的,展示是直接將這個html放在對應的區域內就可以,然而,在寫接口是,另外一方需要純文本的,而我頁面沒有運用這個插件,怎么做?最終得以解決,希望對需要的人有用。
html文本:
<p style="margin: auto; padding: inherit; font-stretch: normal; line-height: 1.8; font-family: tahoma, "microsoft yahei", 微軟雅黑; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"> 引用<a href="http://www.divcss5.com/" style="color: rgb(0, 0, 204);">CSS</a>文件到<a href="http://www.divcss5.com/html/" style="color: rgb(0, 0, 204);">Html</a>方法-<strong>css引入</strong>,<strong>css引用</strong> </p> <p style="margin: auto; padding: inherit; font-stretch: normal; line-height: 1.8; font-family: tahoma, "microsoft yahei", 微軟雅黑; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"> 使用不同的方法來引用<a href="http://www.divcss5.com/rumen/r29.shtml" style="color: rgb(0, 0, 204);">css樣式</a>表,最終到達的效果相同,但是使用不同方法應用的<a href="http://www.divcss5.com/rumen/r72.shtml" style="color: rgb(0, 0, 204);">css文件</a>將影響到SEO及網頁打開速度效率。 </p> <p style="margin: auto; padding: inherit; font-stretch: normal; line-height: 1.8; font-family: tahoma, "microsoft yahei", 微軟雅黑; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"> html引用css方法如下<br/>1、直接在<a href="http://www.divcss5.com/" style="color: rgb(0, 0, 204);">div</a>中使用css樣式制作<a href="http://www.divcss5.com/" style="color: rgb(0, 0, 204);">div+css</a>網頁<br/>2、html中使用style自帶式<br/>3、使用@import引用外部CSS文件<br/>4、使用<a href="http://www.divcss5.com/html/h64.shtml" style="color: rgb(0, 0, 204);">link</a>引用外部CSS文件 推薦此方法 </p> <p style="margin: auto; padding: inherit; font-stretch: normal; line-height: 1.8; font-family: tahoma, "microsoft yahei", 微軟雅黑; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"> <span style="color: rgb(255, 0, 0);">擴展閱讀</span>:<a href="http://www.divcss5.com/rumen/r431.shtml" style="color: rgb(0, 0, 204);">link與import區別</a> </p> <p style="margin: auto; padding: inherit; font-stretch: normal; line-height: 1.8; font-family: tahoma, "microsoft yahei", 微軟雅黑; color: rgb(51, 51, 51); white-space: normal; background-color: rgb(255, 255, 255);"> 接下來我們將逐個講解html引用css方法的例子 </p> <p> <br/> </p>
我們現在運用正則表達式對這個html文本進行處理,使用正則表達式可以最快速的過濾到html標簽,這個方法遺留的問題就是有時候空格不清楚是用戶敲的還是其他原因放上去的,在本方法中我將所有空格過去掉了,如果你不需要可以去掉那句代碼,代碼如下:
//將html轉換為純文本,此方法最后保留了&nbps空格,使用時注意將空格替換掉
public static String delHTMLTag(String htmlStr){
String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定義script的正則表達式
String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定義style的正則表達式
String regEx_html="<[^>]+>"; //定義HTML標簽的正則表達式
Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
Matcher m_script=p_script.matcher(htmlStr);
htmlStr=m_script.replaceAll(""); //過濾script標簽
Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
Matcher m_style=p_style.matcher(htmlStr);
htmlStr=m_style.replaceAll(""); //過濾style標簽
Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
Matcher m_html=p_html.matcher(htmlStr);
htmlStr=m_html.replaceAll(""); //過濾html標簽
return htmlStr.trim(); //返回文本字符串
}
最后的字符串就是如下圖所示這樣:
引用CSS文件到Html方法-css引入,css引用使用不同的方法來引用css樣式表,最終到達的效果相同,但是使用不同方法應用的css文件將影響到SEO及網頁打開速度效率。html引用css方法如下1、直接在div中使用css樣式制作div+css網頁2、html中使用style自帶式3、使用@import引用外部CSS文件4、使用link引用外部CSS文件推薦此方法擴展閱讀:link與import區別接下來我們將逐個講解html引用css方法的例子
簡單的記錄下來,表達可能不清晰希望大家不要介意!