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

热点内容
净水电导率怎么计算 浏览:547
明月镜片树脂 浏览:558
冷水鱼用什么代替净水剂最好 浏览:193
城市污水处理有什么影响 浏览:63
蒸馏葡萄酒口感 浏览:873
抽奖的净化器怎么保修 浏览:220
背包过滤设计图 浏览:303
家庭水处理小设备价格 浏览:727
广州人工湿地污水处理多少钱 浏览:2
挂式饮水机是什么价格呢 浏览:204
提升器利弊 浏览:890
动物细胞的半透膜是指 浏览:755
空压机滤芯烂了怎么回事 浏览:369
白沙县邦溪村污水项目 浏览:813
净水器废水比最高 浏览:657
污水管有流沙怎么补 浏览:426
史密斯即热式饮水机怎么样 浏览:835
水垢重的水喝了 浏览:504
机油滤芯是怎么排气 浏览:285
日本铁壶的水垢该清理吗 浏览:325