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

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. 廖雪峰ES6的箭头函数

    http://www.liaoxuefeng.com/ wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/0014385659690576 ...

  2. 生产环境下搭建mongodb复制集高可用环境(python)

    环境描述:有三台ubuntu服务器,,每台服务器上已经有mongodb实例.创建3个mongo2.4的新实例,分别作为三个复制集节点,同时保证了当前单节点环境的稳定 3台服务器都已经有单个mongo实 ...

  3. 英特尔、联发科、展讯等开始支持开源的物联网轻量化操作系统AliOS Lite

    操作系统AliOS Lite Chaos 12-21 11:03 在 12 月 20 日的云栖大会北京峰会上,阿里宣布即将开源 AliOS Lite,此前面向 IoT 领域的轻量级物联网嵌入式操作系统 ...

  4. python自动化运维-编写rsync+sersync安装脚本实现文件实时同步

    rsync+sersync组合可以实时监听目录的变化,实现实时同步数据. 具体安装教程可查看:http://www.osyunwei.com/archives/7447.html. 安装着实有些复杂, ...

  5. CodeBlocks+Qt(MinGW)配置

    1.安装CodeBlocks 官网:http://www.codeblocks.org/ 下载地址:http://www.codeblocks.org/downloads/26 有以下两种选择 cod ...

  6. 《Eye In-Painting with Exemplar Generative Adversarial Networks》论文阅读笔记

    Abstract 基于conditional GAN使用隐藏在reference image中的exemplar information生成high-quality,personalized in-p ...

  7. 02_通过scrollview实现内容滚动

    在ScrollView里面放一个Button和TextView程序直接就挂了. ScrollView它只限制了有几个孩子,没限制有几个孙子.给Button和TextView套上一个爹LinearLay ...

  8. bash 脚本编程七 将命令输出保存到变量中(转载)

    转自:http://blog.csdn.net/csfreebird/article/details/7978699 `符号包含的命令执行完后,可以讲其输出结果保存到变量中 #!/bin/bash v ...

  9. E20180406-hm

    dressing n. 穿衣; 调味品; 肥料 dress n. 衣服; 礼服; 连衣裙; 装饰; vt. 打扮; 穿着; 给…穿衣; monde  n. 时髦社交界,上流社会; ingredient ...

  10. hdoj5387【模拟】

    题意: 略: 思路: 把所有的角度按照分母的形式写,中间不要约,不要除...(然后我就wa了),本来是想保证结果的正确性,最后会造成约好以后分子很大..>360°: /* 这个案例不错,妈的,随 ...