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

熱點內容
樹脂粉使用說明與方法 瀏覽:176
蒸餾水可以坐電瓶液嗎 瀏覽:459
什麼牌子的飲水機過濾好 瀏覽:257
往海洋排放核廢水會有什麼後果 瀏覽:822
污水留樣時間要求 瀏覽:79
蹲廁過濾漏 瀏覽:654
反滲透膜ge美國 瀏覽:850
乙醇浸提蒸餾法 瀏覽:994
沒弓怎麼打純水 瀏覽:900
雙臂焊煙凈化器怎麼樣 瀏覽:879
環氧樹脂絕緣條 瀏覽:805
凈水器電瓶如何清洗 瀏覽:293
新款凱美瑞汽油濾芯在哪裡 瀏覽:880
超純水一體機怎麼調節單位 瀏覽:605
污水用於農用灌溉的標准 瀏覽:255
增透樹脂 瀏覽:995
南京高空排放油煙凈化器多少錢 瀏覽:195
0017是什麼型樹脂 瀏覽:587
反滲透膜濾芯安裝和取出圖片 瀏覽:690
電鍍污水含什麼 瀏覽:190