Categorias

Remover Tags HTML de uma String com Expressão Regular

Com esta função é possível remover todas as Tags HTML de uma string utilizando Expressão Regular.

Essa função pode ser bastante útil quando é preciso tratar informações submetida por algum usuário sem a “sujeira” das tags.

<%
'-----------------------------------------------------
'Nome: RemoveHTMLTags(ByVal strHTML)
'Tipo: Funcao
'Sinopse: Remove todas as tags HTML de uma string
'Parametros:
'   strHTML: String com as tags HTML a serem retiradas
'Retorno: String
'Autor: Gabriel Fróes - www.codigofonte.com.br
'-----------------------------------------------------
Function RemoveHTMLTags(ByVal strHTML)
    Dim objER
    Dim strTexto

    'Configurando o objeto de Expressão Regular
    Set objER            = New RegExp 
    objER.IgnoreCase    = True
    objER.Global        = True
    objER.Pattern        = "<[^>]*>"
    
    'Substituindo as tags encontradas pela expressão
    strTexto            = strHTML
    strTexto            = objER.Replace(strTexto, "")
    
    Set objER            = Nothing

    'Retornando a função
    RemoveHTMLTags = strTexto
End Function

'-----------------------------------------------------
'Exemplo de chamada da função
'-----------------------------------------------------
Dim HTML
HTML = "teste da função de retira as TAGS HTML
" 'Texto com TAG Response.Write HTML 'Texto sem TAG Response.Write RemoveHTMLTags(HTML) %>