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

热点内容
纯水的电离情况是什么意思 浏览:694
小分子量环氧树脂 浏览:587
换了树脂还显示电离交换期 浏览:70
格兰富废污水提升泵安装 浏览:572
树脂复合井盖容易碎吗 浏览:459
饮水机底部不锈钢直管怎么堵住 浏览:544
污水流量怎么按面积计算 浏览:28
为什么欧派净水器没有完全净化 浏览:771
北京小型减压蒸馏反应釜 浏览:991
河北废水处理氨氮超标如何处理 浏览:938
纯水机后置弹出黄色的水怎么回事 浏览:793
年年都有来收污水排污 浏览:854
二级反渗透进水加减 浏览:183
水蒸气蒸馏有机物水蒸气比例 浏览:24
生活废水排污许可 浏览:875
牡丹饮水机指示灯线怎么接 浏览:114
反渗透7只膜壳怎么分段 浏览:386
飞得滤机空气滤芯质量如何 浏览:6
黑车身上的白色水垢 浏览:433
细菌过滤器会不会使溶质减少 浏览:433