ASP删除img标签的style属性只保存src的正则函数

ASP去除img标签style属性只保存src

'成果:将IMG代码名目化为<img src="https://www.jb51.net/article/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=""https://www.jb51.net/article/$1"" />")     '//正则匹配图片SRC地点     regEx.Pattern = "<img.*?\ssrc=([\""\'])([^\""\']+?)\1.*?>"     sReallyDo = regEx.Replace(sReallyDo, "<img src=""https://www.jb51.net/article/$2"" />")     FixImg = sReallyDo End Function

上面的代码确实写得不错,道理也可以参考下面的代码

js过滤img里style属性

var str = "<img>" 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属性内容请搜索剧本之家以前的文章或继承欣赏下面的相关文章但愿各人今后多多支持剧本之家!

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsdffy.html