使用正则表达式拼接富文本框

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页面属性的替换的更多相关文章

  1. 【GeneXus】在WorkWithPlus中如何定义未被包含的页面属性?

    在使用GeneXus开发项目的过程中,有很多用户会使用到WorkWithPlus这个模板.通过WorkWithPlus的编辑器,让页面的调整变得极为简单,尤其是响应式页面.在WorkWithPlus的 ...

  2. 7月新的开始 - Axure学习02 - 页面属性、钢笔工具

    页面属性 页面属性可以修改整个页面的效果 包含: 属性.对交互用力和事件的编辑 样式.对页面的样式操作 说明.可以对整个页面进行说明.以及样式的说明 钢笔工具:锚点.路径 锚点:钢笔点击之后的点就是锚 ...

  3. window.location.href/replace/reload()--页面跳转+替换+刷新

    一.最外层top跳转页面,适合用于iframe框架集 top.window.location.href("${pageContext.request.contextPath}/Login_g ...

  4. 正则中的lastIndex属性

    首先大家看下下面的代码 var reg = /\d/; console.log( reg.test("1") ); console.log( reg.test("1&qu ...

  5. phpstorm 正则匹配删除注释行(替换注释行为空行)

    使用phpstorm 来编写php 和javascript 代码,感觉还是不错的,用得也很舒服. 遇到了一个需求,有时候在阅读框架源代码的时候 , 想过滤(删除)掉源代码中的注释行,如果手动逐行删除显 ...

  6. 5. window.location.href/replace/reload()--页面跳转+替换+刷新

    1.window.location=url; window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面. 一.最外层top跳转页面,适合用于iframe框架集 ...

  7. Spring.net页面属性注入

    .条件spring.web程序集 1.1 system.web配置 <httpHandlers> <add verb="*" path="*.aspx& ...

  8. iOS的Runtime机制下给类别(category)添加属性、替换原有类的方法执行

    一.Runtime的理解 OC是面向对象的语言这是常识,其实就是通过Runtime机制动态创建类和对象,这里只是简单的运用runtime的使用! 二.类别(category)添加属性_使用前记得导入头 ...

  9. JSP页面属性

    一.JSP指令 <%@指令名属性名=属性值 %> page指令: 定义页面是如何解析 include指令: 静态包含 taglib指令: 在页面引入标签呢库. 1.page指令属性 imp ...

随机推荐

  1. sscanf在字符串中的一些使用

    弟弟的作业 你的弟弟刚做完了"100以内数的加减法"这部分的作业,请你帮他检查一下.每道题目(包括弟弟的答案)的格式为a+b=c或者a-b=c,其中a和b是作业中给出的,均为不超过 ...

  2. Laravel配置nginx环境

    前言: 之前坑的!一直在尝试配置,但都失败了,只能用着apache,但是最近想整合swoole到laravel,无奈当前测试服务器是nginx,我只能再尝试在nginx上搭laravel环境 方法如下 ...

  3. Code-zabbix:zabbix-3.4-快速入门

    ylbtech-Code-zabbix:zabbix-3.4-快速入门 1.返回顶部 1. 1 登陆和配置用户 登陆Zabbix,以及在Zabbix内建立一个系统用户. 用户名:Admin 或者 ad ...

  4. bzoj4556

    后缀自动机+二分+倍增+线段树合并 后缀自动机真好用 后面一个串是固定的,那么我们要对前面的串进行一些操作.我们想既然是求lcp,那么我们得先翻转原串,这样前缀变成了后缀,然后二分一下,从d在自动机上 ...

  5. ORA-00020: maximum number of processes (xxxx) exceeded 报错解决方法

    转自:http://blog.51cto.com/lee90/1788124 今天java开发在连接线上的oracle大量导数据,一会提示连接不上数据库了.我本地用sqldeveloper也连接不上. ...

  6. Android中shape的使用 (转载)

    转自:http://blog.csdn.net/ekeuy/article/details/12349853 在看很多开源代码中都使用到了shape,我看代码的时候一般都一带而过了,没有仔细去研究,这 ...

  7. 深度解密Go语言之 map

    目录 什么是 map 为什么要用 map map 的底层如何实现 map 内存模型 创建 map 哈希函数 key 定位过程 map 的两种 get 操作 如何进行扩容 map 的遍历 map 的赋值 ...

  8. POJ1700 【经典过河问题,贪心】

    题意: n个人过河, 船每次只能坐两个人, 然后船载每个人过河的所需时间不同, 问最快的过河时间. 思路: 仅仅启发一下思维: 我相信很多人一下子的想法就会有,每次最快和那些慢的过去,然后让最快一直来 ...

  9. hdoj5667 BestCoder Round #80 【费马小定理(膜拜)+矩阵快速幂+快速幂】

    #include<cstdio> #include<string> #include<iostream> #include<vector> #inclu ...

  10. hdoj【1006】【未完待续】

    题意: 时钟的三个指针在两两之间至少D°的时候是开心的,求开心时间占一天的一个百分比. 思路: 我们看到这个百分比有三位小数,精度很高. 一天有24h,24*60min,24*60*60s,那么最小单 ...