1. 程式人生 > 程式設計 >ASP刪除img標籤的style屬性只保留src的正則函式

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,vbTab,"<img ",vbCrLf & "<img ",1,-1,1)
sReallyDo = Replace(sReallyDo,"/>"," />",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屬性內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!