2015-01-05 62 views
0

我想從字符串中去除所有的HTML標籤,只保留特定的(保留標籤和屬性), 我有這樣的: 地帶HTML標籤,保持特定

set objRegExp = new RegExp 
with objRegExp 
.Pattern = "<^((b)|(i)|(em)|(strong)|(br)|(img))>.*</.*>" 
.Global = True 
end with 

和使用:

objRegExp.replace(request.form("content"), "") 

不會改變任何東西。

我需要這個爲我建立的論壇,它支持所見即所得編輯器,我想防止xss & sql注入。

+0

這裏沒有VB6。問題被誇大了。 – Bob77

回答

2

要去除所有的HTML標籤:

Public Function RegexAllHtml(strValue) 
    Set RegularExpressionObject = New RegExp 
    With RegularExpressionObject 
    .Pattern = "<(.|\n)+?>" 
    .IgnoreCase = True 
    .Global = True 
    End With 
    Dim strResult: strResult = RegularExpressionObject.Replace(strValue, " ") 
    Set RegularExpressionObject = Nothing 

    RegexAllHtml = strResult 
End Function 

刪除特定的標籤(例如SPAN),你可以使用類似: <SPAN[^><]*>|<.SPAN[^><]*>

,或保持特定的標記(如B中的粗體) :<(?!/?(?:strong|b)\b)[^>]*>

順便說一句:大多數WYSIWG編輯器都允許您配置哪些標記不安全,然後在保存內容之前將其刪除!例如見CKEditor:http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-allowedContent