使用正则进行HTML页面属性的替换
使用正则表达式拼接富文本框
package com.goboosoft.common.utils; import org.apache.commons.lang3.StringUtils; import java.util.regex.Matcher;
import java.util.regex.Pattern; /**
* Description:
*
* @author cy
* @date 2019年04月01日 17:35
* version 1.0
*/
public class HtmlUtils { /**
* 替换指定标签的属性和值
* @param str 需要处理的字符串
* @param tag 标签名称
* @param tagAttrib 要替换的标签属性值
* @param startTag 新标签开始标记
* @param endTag 新标签结束标记
* @return
* @author huweijun
* @date 2016年7月13日 下午7:15:32
*/
public static String replaceHtmlTag(String str, String tag, String tagAttrib, String startTag, String endTag) {
String regxpForTag = "<\\s*" + tag + "\\s+([^>]*)\\s*" ;
String regxpForTagAttrib = tagAttrib + "=\\s*\"([^\"]+)\"" ;
Pattern patternForTag = Pattern.compile (regxpForTag,Pattern. CASE_INSENSITIVE );
Pattern patternForAttrib = Pattern.compile (regxpForTagAttrib,Pattern. CASE_INSENSITIVE );
Matcher matcherForTag = patternForTag.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result = matcherForTag.find();
while (result) {
StringBuffer sbreplace = new StringBuffer( "<"+tag+" ");
Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag.group());
if (matcherForAttrib.find()) {
String attributeStr = matcherForAttrib.group();
matcherForAttrib.appendReplacement(sbreplace, startTag + attributeStr + endTag);
}
matcherForAttrib.appendTail(sbreplace);
matcherForTag.appendReplacement(sb, sbreplace.toString());
result = matcherForTag.find();
}
matcherForTag.appendTail(sb);
return sb.toString();
} public static String replaceImgSrc(String content,String domain){
if(StringUtils.isBlank(content)){
return null;
}
String buf = "src=\"" + domain;
String s = replaceHtmlTag(content, "img", "src", buf, "\"");
return s;
} public static void main(String[] args) {
StringBuffer content = new StringBuffer();
content.append("<ul class=\"imgBox\"><li><img id=\"160424\" src=\"uploads/allimg/160424/1-160424120T1-50.jpg\" class=\"src_class\"></li>");
content.append("<li><img id=\"150628\" src=\"uploads/allimg/150628/1-15062Q12247.jpg\" class=\"src_class\"></li></ul>");
System.out.println("原始字符串为:"+content.toString());
String s = replaceImgSrc(content.toString(), "http://files.goboosoft.com/zwjm/");
System.out.println("替换后为:"+s);
} }
/**
* 去除图片中的domain
* @param htmlStr html字符串
* @return String
*/
private static String deleteImgSrcDomain(String htmlStr) {
List<String> pics = new ArrayList<String>();
String img = "";
String repimg = "";
Pattern p_image;
Matcher m_image;
String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
m_image = p_image.matcher(htmlStr);
while (m_image.find()) {
// 得到<img />数据
img = m_image.group();
// 匹配<img>中的src数据
Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
while (m.find()) {
String s = m.group();
pics.add(s);
// 处理图片信息
String substring = s.substring(s.lastIndexOf("/") + , s.length());
repimg = img.replace(s, substring);
htmlStr = htmlStr.replace(img, repimg);
img = repimg;
}
}
return htmlStr;
}
使用正则进行HTML页面属性的替换的更多相关文章
- 【GeneXus】在WorkWithPlus中如何定义未被包含的页面属性?
在使用GeneXus开发项目的过程中,有很多用户会使用到WorkWithPlus这个模板.通过WorkWithPlus的编辑器,让页面的调整变得极为简单,尤其是响应式页面.在WorkWithPlus的 ...
- 7月新的开始 - Axure学习02 - 页面属性、钢笔工具
页面属性 页面属性可以修改整个页面的效果 包含: 属性.对交互用力和事件的编辑 样式.对页面的样式操作 说明.可以对整个页面进行说明.以及样式的说明 钢笔工具:锚点.路径 锚点:钢笔点击之后的点就是锚 ...
- window.location.href/replace/reload()--页面跳转+替换+刷新
一.最外层top跳转页面,适合用于iframe框架集 top.window.location.href("${pageContext.request.contextPath}/Login_g ...
- 正则中的lastIndex属性
首先大家看下下面的代码 var reg = /\d/; console.log( reg.test("1") ); console.log( reg.test("1&qu ...
- phpstorm 正则匹配删除注释行(替换注释行为空行)
使用phpstorm 来编写php 和javascript 代码,感觉还是不错的,用得也很舒服. 遇到了一个需求,有时候在阅读框架源代码的时候 , 想过滤(删除)掉源代码中的注释行,如果手动逐行删除显 ...
- 5. window.location.href/replace/reload()--页面跳转+替换+刷新
1.window.location=url; window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面. 一.最外层top跳转页面,适合用于iframe框架集 ...
- Spring.net页面属性注入
.条件spring.web程序集 1.1 system.web配置 <httpHandlers> <add verb="*" path="*.aspx& ...
- iOS的Runtime机制下给类别(category)添加属性、替换原有类的方法执行
一.Runtime的理解 OC是面向对象的语言这是常识,其实就是通过Runtime机制动态创建类和对象,这里只是简单的运用runtime的使用! 二.类别(category)添加属性_使用前记得导入头 ...
- JSP页面属性
一.JSP指令 <%@指令名属性名=属性值 %> page指令: 定义页面是如何解析 include指令: 静态包含 taglib指令: 在页面引入标签呢库. 1.page指令属性 imp ...
随机推荐
- POJ2443 Set Operation (基础bitset应用,求交集)
You are given N sets, the i-th set (represent by S(i)) have C(i) element (Here "set" isn't ...
- Python的多进程锁的使用
很多时候,我们需要在多个进程中同时写一个文件,如果不加锁机制,就会导致写文件错乱 这个时候,我们可以使用multiprocessing.Lock() 我一开始是这样使用的: import multip ...
- mac上python3安装HTMLTestRunner
下载支持python3版本的HTMLTestRunner.py文件后,拷贝到python3的lib目录下 在终端输入如下命令: 将HTMLTestRunner.py文件拷贝到如下目录后,pycharm ...
- 【转】IntelliJ IDEA2017.3激活
原文地址:https://blog.csdn.net/qq_27686779/article/details/78870816 http://idea.java.sx/ 简单快捷!! ———————— ...
- 堆、栈的区别 <转载>
本篇非作者原创,转子链接,仅供学习记录. 一.预备知识—程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值 ...
- Button Style
Button Style BS_3STATE 与复选框一样本样式按钮可被单击变暗.变暗状态通常用于指示本样式的按键正处于禁用状态. BS_AUTO3STATE 与三状态的复选框一样当用户选中它本按钮样 ...
- 【212】HDF更新数据&HDF创建
HDF更新数据:对原有HDF数据进行数据更新,不破坏HDF的数据结构 pro add_data_sst ;实现将SST增加1度,再将结果更新到SST中 ;1. 获取SST索引 ;2. 通过索引获取ID ...
- gulp 实现 js、css,img 合并和压缩(转)
前提条件,知道如何安装nodejs.gulp,这里不做介绍,可以自行google 实现此功能需要安装的gulp工具有如下 npm install gulp-htmlmin gulp-imagemin ...
- CodeForces 718C && HDU 3572 && Constellation
Point 1. 区间乘以一个数/矩阵的幂时,不要用指数相加的方法. 而要用直接维护mulv[x]表示区间要乘多少. 不然的话,空加一个logn 2. 要点在于,冲突的点连边,形成二分图,对于在同一个 ...
- Codechef SEPT17
Codechef SEPT17 比赛链接:https://www.codechef.com/SEPT17 CHEFSUM code给定数组 a[1..n] ,求最小的下标 i ,使得 prefixsu ...