使用正则进行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 ...
随机推荐
- 【Cocos2dx】新建场景、场景的切换、设置启动场景与菜单的新建
这是Cocos2dx最简单的部分.主要是体现对场景的操作,其实这东西就是Flash的舞台,安卓的Activity,WIN32窗体程序的Framework窗体,网页的body,反正就是对那个容纳各种东西 ...
- WaveNet: 原始音频生成模型
官方博客 WaveNet: A Generative Model for Raw Audio paper地址:paper Abstract WaveNet是probabilistic and auto ...
- C#面向对象之数据库(理论、插入、修改、删除、查询)
1.数据库的作用:不仅仅是存储,更重要的是将数据进行存储以后怎么样才能方便快捷的查询修改 2.数据库的特点:海量存储.查找速度快.并发性问题控制.安全性.数据完整性(保存在数据库中的数据是正确的.真是 ...
- mysql5.7 异常ERROR 1055 (42000)
大致错误如:ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonagg ...
- hihocoder 1582 : Territorial Dispute(凸包)
传送门 题意 略 分析 求一个凸包即可 1.所有点在凸包上且点数>3,令凸包上第1,3点为'A',其余点为'B' 2.部分点在凸包上,令凸包上点为'A',其余点为'B' 3.无可行情况 附代码 ...
- Python 字符串太长分行写
原文:https://blog.csdn.net/peng__dada/article/details/79138135 #第一种:三个单引号 print '''我是一个程序员 我刚开始学 ...
- 使用selesium和pytesseract识别验证码,达到登录网页目的
关于验证码问题,大多可以在网上了解到目前有四种解决方案:1.开发注释验证码2.开发开一个“后门”,设置一个万能码,输入万能码则通过3.通过cookies绕过验证码4.图形识别技术 前三种是比较快速也是 ...
- iOS 设置UITextView的Placeholder
代码如下: - (void)setupTextView { UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, ...
- Hexo搭建博客教程(3) - 远程部署到GitHub Pages
本章讲的是如何将本地的个人项目远程部署到 GitHub Pages,涉及到GitHub的项目仓库.Git的使用,以及Hexo的远程部署等. 1. 安装 hexo-deployer-git 插件 想要将 ...
- [JLOI2008]将军
Description 刘先生最近在学习国际象棋,使用一个叫"jloi-08"的游戏软件.在这个游戏里,不但可以和电脑普通地对弈,还可以学习著名的棋局,还有针对初学者的规则指导等丰 ...