ASP删除img标签的style属性只保留src的正则函数
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="" }
相关推荐
有梦就能实现 2020-06-05
gcgl000hugang 2020-02-10
hellobabygogo 2020-03-06
wbingyang 2020-02-16
一对儿程序猿 2019-12-22
宿舍 2019-12-11
liangkaiping0 2011-09-23
niuyongaiai 2013-06-09
drise 2012-05-29
不懂 2019-06-28
shqhope 2020-06-23
WebVincent 2020-06-16
lynjay 2020-06-14
ajaxtony 2020-06-11
zhendeshifeng 2020-05-27
Yasin 2020-05-05
Darklovy 2020-04-16
程松 2020-02-23