ASP删除img标签的style属性只保留src的正则函数
程序员文章站
2022-06-09 17:06:41
asp去除img标签style属性只保留src'功能:将img代码格式化为格式,只保留src属性,去掉img标签的其他属性。上面的代码确实写得不错,原理也...
asp去除img标签style属性只保留src
'功能:将img代码格式化为<img src="xxx" />格式,只保留src属性,去掉img标签的其他属性。
function fiximg(sstring) dim sreallydo, regex, ireallydo dim omatches, cmatch dim tstarttime, tendtime if isnull(sstring) then fiximg = "" exit function end if sreallydo = sstring on error resume next sreallydo = replace(sreallydo, vbcr, " ") sreallydo = replace(sreallydo, vblf, " ") sreallydo = replace(sreallydo, vbtab, " ") sreallydo = replace(sreallydo, "<img ", vbcrlf & "<img ", 1, -1, 1) sreallydo = replace(sreallydo, "/>", " />", 1, -1, 1) sreallydo = replaceall(sreallydo, "= ", "=", true) sreallydo = replaceall(sreallydo, "> ", ">", true) sreallydo = replace(sreallydo, "><", ">" & vbcrlf & "<") sreallydo = trim(sreallydo) on error goto 0 set regex = new regexp regex.ignorecase = true regex.global = true '//去除onclick,onload等脚本 regex.pattern = "\s[on].+?=([\""|\'])(.*?)\1" sreallydo = regex.replace(sreallydo, "") '//将src不带引号的图片地址加上引号 regex.pattern = "<img.*?\ssrc=([^\""\'\s][^\""\'\s>]*).*?>" sreallydo = regex.replace(sreallydo, "<img src=""$1"" />") '//正则匹配图片src地址 regex.pattern = "<img.*?\ssrc=([\""\'])([^\""\']+?)\1.*?>" sreallydo = regex.replace(sreallydo, "<img src=""$2"" />") fiximg = sreallydo end function
上面的代码确实写得不错,原理也可以参考下面的代码
js过滤img里style属性
var str = "<img style='ddddddddd'>" str = str.replace(/<img[^>]*>/gi, function (match, capture) { return match.replace(/style\s*?=\s*?([‘"])[\s\s]*?\1/ig, '') })) console.log(str)
不是替换直接通过js设置
var is=document.images; for(var i=0,len=is.length;i<len;i++){ is[i].style.csstext="" }
到此这篇关于asp删除img标签的style属性只保留src的正则函数的文章就介绍到这了,更多相关asp去除img的style属性内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!