C#/JS 利用正则表达式 替换/删除 img 里面的 width height
来源:互联网 发布:药店用的软件 编辑:程序博客网 时间:2024/05/01 07:40
JS:
function test() { var str = "<img title=\"\" alt=\"\" align=\"\" src=\"/kindeditor/attached/image/20161214/20161214162554_8001.jpg\" width=\"312\" height=\"312\" />"; str += "<img title=\"\" alt=\"\" align=\"\" src=\"/kindeditor/attached/image/20161214/20161214162554_8001.jpg\" width=\"412\" height=\"412\" />"; str = str.replace(/ height="\d+"/g, " height=\"90\"");// “/”后面的是要替换的字符,“d\+”是数字,最后""里是用来填充的字符 str = str.replace(/ width="\d+"/g, " width=\"90\""); alert(str); }
<input type="button" id="hh" onclick="test();" value="hell" />
C#:
var str = "<img title=\"\" alt=\"\" align=\"\" src=\"/kindeditor/attached/image/20161214/20161214162554_8001.jpg\" width=\"312\" height=\"312\" />";str += "<img title=\"\" alt=\"\" align=\"\" src=\"/kindeditor/attached/image/20161214/20161214162554_8001.jpg\" width=\"412\" height=\"412\" />";Regex reg = new Regex("width\\s*=\\s*\\S+ height\\s*=\\s*\\S+"); string result = reg.Replace(str, "width=\"90\" height=\"90\""); 第二种方法替换 //Regex reg1 = new Regex("height\\s*=\\s*\\S+");//string result1 = reg1.Replace(result, "height=\"90\"");
处理多张图片中出现没有width height的情况:
//正则替换图片的宽高 Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase); MatchCollection matches = regImg.Matches(item.AdImg); var relImg = string.Empty; foreach (Match m in matches) { //进行匹配img是否存在width height Regex reg = new Regex("width\\s*=\\s*\\S+ height\\s*=\\s*\\S+"); if (!reg.IsMatch(m.Value))//img 中不存在width height { //获取其他属性进行替换 Regex reg1 = new Regex("alt\\s*=\\s*\\S+"); relImg += reg1.Replace(m.Value, "style=\"width:90px;height:90px;\""); } else { relImg+= reg.Replace(m.Value, "style=\"width:90px;height:90px;\""); } }
0 0
- C#/JS 利用正则表达式 替换/删除 img 里面的 width height
- 在php中用正则表达式删除img标签的width、height、style
- java 字符串匹配 删除 img 的width height属性
- img的width和height属性
- 去掉img标签里的width 和 height的属性
- Java正则表达式替换Img标签的src属性
- java 正则表达式替换img标签的src路径
- 正则表达式替换img标签src值
- Js正则表达式替换
- JS里面的正则表达式教学
- java正则除去img中的height和width保留src属性
- js 利用正则表达式替换字符串中指定字符
- 关于chrome,firefox中img.width=0,img.height=0的解决方法
- js 正则表达式 获得img 的src属性
- 利用正则表达式替换功能,将C++注释转变成纯C的注释
- img 的正则表达式:匹配<img>标签
- 如何利用DOM获取当前页面的width和height?
- js正则表达式替换空格
- poj 2299 c++:Ultra-QuickSort
- 简易记账app
- Android动画-Frame Animation(帧动画)
- Ubuntu 16.04安装有道词典的方法
- 51Nod 1418 放球游戏
- C#/JS 利用正则表达式 替换/删除 img 里面的 width height
- acm字母小游戏
- 绑定socket到指定网卡
- 判断浏览器版本
- Pandas下的DataFrame的数据选择、切片
- Dubbo(三) 聚合工程之Service层接口Demo
- 【24.67%】【codeforces 551C】 GukiZ hates Boxes
- 图像处理之—振铃现象
- 洛谷最大子树和解题报告