導航:首頁 > 凈水問答 > 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相關的資料

熱點內容
污水閘門斷了怎麼辦 瀏覽:66
家樂士凈水器男普工都做什麼 瀏覽:678
白色過濾嘴香煙圖 瀏覽:523
上海超濾膜廠家 瀏覽:540
污水管道有油污怎麼辦 瀏覽:22
圖書館飲水機是什麼飲水機 瀏覽:34
汽車空氣濾芯怎麼拿下來 瀏覽:650
小型門診污水處理設備性價比高 瀏覽:565
強陰離子交換硅膠色譜柱 瀏覽:508
如何看濾芯是1微米還是5微米 瀏覽:566
去除花灑中的水垢 瀏覽:987
空調顯示凈化器怎麼樣 瀏覽:11
xsmax主卡來電怎麼用副卡回撥 瀏覽:139
剛打的耳洞進污水怎麼辦 瀏覽:544
流動床離子交換 瀏覽:618
1800瓦污水泵 瀏覽:155
污水處理怎麼加助凝劑 瀏覽:406
廢水裡有硫酸怎麼處理 瀏覽:873
一個人一天可以用多少污水 瀏覽:299
污水塘里能養什麼魚 瀏覽:600