導航:首頁 > 凈水問答 > java過濾html保留img

java過濾html保留img

發布時間:2025-01-02 15:00:27

⑴ java如何去掉字元串中的 html標簽

1.去除單個HTML標記
String s="asdfasd<script>asdfsfd</script>1234";
System.out.println(s.replaceAll("<script.*?(?<=/script>)",""));
2.去除所有HTML標記
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HTMLSpirit{ ITjob 遠標教育
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(); //返迴文本字元串
}
}

⑵ 求一個JAVA替換HTML裡面<img>標簽裡面絕對路徑的部分字元串正規則表達式

html.replaceall("<IMG SRC=\\".*?\\"/>","your string")

⑶ 怎麼清除所有的html標簽,(包括圖片、</p>、<span>等等所有標簽)只保留文字部分

這是我以前整理的,你可以先試試
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HTMLSpirit{
publicstatic 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(); //返迴文本字元串
}
}

⑷ java正則表達式過濾html p標簽

用JavaScript方法如下,JAVA語言類似:
'你的HTML文本'.replace(/.+>(.+)<.+/,'$1')

⑸ java 如何利用正則表達式只保留html裡面的<p></p>標簽裡面的內容

正則表達式:<p.*?>(.*?)</p>
group(1)為正文內容。
輸出時加上\n就行了

import java.util.regex.*;
public class Test{
public static void main(String[] args){
String str="<p style=\"font-size:1.3em;font-weight:bold\">No page with that title exists.</p> ";
String regex="<p.*?>(.*?)</p> ";
Pattern p =Pattern.compile(regex);
Matcher m=p.matcher(str);
while(m.find()){
System.out.println(m.group(1));
}
}
}

閱讀全文

與java過濾html保留img相關的資料

熱點內容
工業污水銅離子超標解決方法 瀏覽:643
通遼二手煙空氣凈化器多少錢一台 瀏覽:746
東平縣第二污水處理廠在哪 瀏覽:970
污水處理廠積泥問題 瀏覽:243
起亞k3空氣凈化器怎麼使用 瀏覽:695
空氣凈化器ccm之什麼 瀏覽:48
樹脂版175線 瀏覽:33
福建生活污水處理用什麼設備 瀏覽:885
加強醫療污水 瀏覽:826
微粉過濾器 瀏覽:187
空氣凈化器負離子怎麼測 瀏覽:952
小米凈化器亮黃燈什麼意思 瀏覽:273
一體化污水處理設施施工方案 瀏覽:507
蒸餾水是鹼還是酸 瀏覽:528
朗逸空氣濾芯選什麼型號 瀏覽:78
層析用聚醯胺樹脂耐熱性 瀏覽:616
飛度空氣濾芯卡扣去哪裡買 瀏覽:887
加熱純凈水為什麼會有沫沫 瀏覽:425
哪些花草可除污水 瀏覽:53
雲米濾芯起什麼作用 瀏覽:738