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

热点内容
蒸馏酒许可证怎么办6 浏览:484
美的饮水机浮球组件怎么放 浏览:664
济南水处理厂有限公司 浏览:868
洗衣柜的清污水如何排管 浏览:735
城市污水回用 浏览:240
生物膜对废水的净化过程及原理 浏览:411
阎良水处理环保公司 浏览:918
桑普空气净化器怎么办 浏览:97
北京赛诺超滤膜原件 浏览:841
美国陶氏RO膜是哪里产的 浏览:285
污水管道清理费入什么账 浏览:53
反相色谱为什么不能用纯水 浏览:445
广州光明村污水管工程 浏览:647
全自动咖啡机可以用柠檬酸除垢吗 浏览:68
养绿萝用什么净化器好 浏览:50
英尼克纯水机怎么安装 浏览:379
污水处理中存在的问题调查报告 浏览:58
滤芯里面有蚂蚁怎么办 浏览:939
纯水电导多少合适 浏览:276
汽油滤芯40bar什么意思 浏览:507