导航:首页 > 净水问答 > 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相关的资料

热点内容
反渗透的压力桶是干什么用的 浏览:534
树脂的药效 浏览:840
丹参酮废水 浏览:126
蒸馏提取亚麻籽油 浏览:772
蒸馏水器10lh 浏览:455
电泳漆卷式超滤膜出水混浊 浏览:791
污水检查井可以做方的 浏览:248
川崎400机油滤芯跟什么车一样的 浏览:466
污水改道具体如何实施 浏览:557
树脂补牙痛多久 浏览:988
空气净化器voc老是红色怎么办 浏览:472
商丘水泥雨污水管道 浏览:470
超滤液有什么作用 浏览:765
全国乡镇卫生院污水处理情况 浏览:184
净水器为什么要现在反渗透 浏览:298
请问一级反渗透怎么不出纯水呢 浏览:329
冰蒸馏水和矿泉水的区别 浏览:476
ro水处理工艺一般需要多久 浏览:641
污水闸门断了怎么办 浏览:66
家乐士净水器男普工都做什么 浏览:678