• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

How to remove span tags from html in asp.net?

I have a treeview that has nodes with text that I change the background on by doing the following:

Now to send this data back to the database I need to strip off the <Span> tags
I tried the function below but it doesn't seem to be working:
I am passing in data like so:
text="<span style="backgroundcolor:gray">30606009 : 30</span>   <b></b>"

 Function RemoveHTMLTags(ByVal text)

        Dim regex As New System.Text.RegularExpressions.Regex("<[^>]+>")
        Return regex.Replace(text, String.Empty)

    End Function
Can anyone help me with this?
0
tegronakron
Asked:
tegronakron
  • 2
  • 2
1 Solution
 
svyCommented:
public static string RemoveHtml( object text ) {
      string res = string.Empty;
      if( null != text )
      {
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex( "</?(.*)>",
                              System.Text.RegularExpressions.RegexOptions.IgnoreCase |
System.Text.RegularExpressions.RegexOptions.Multiline );
                        res = regex.Replace( text.ToString(), string.Empty );
      }
      return res;
}
0
 
tegronakronAuthor Commented:
Actually, to make my question more clear.  I want it to only strip off the span tags and return the text inside:
For Example:
input text="<span style="backgroundcolor:gray">30606009 : 30</span>   <b></b>"

output should be
30606009 : 30

0
 
svyCommented:
Try this "<[^>]*>"
0
 
tegronakronAuthor Commented:
That works Great! Thanks!
Here is the final code for others to benefit:

    Public Shared Function RemoveHtmlTags(ByVal [text] As Object) As String
        Dim res As String = String.Empty
        If Nothing <> [text] Then
            Dim regex As New System.Text.RegularExpressions.Regex("<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase Or System.Text.RegularExpressions.RegexOptions.Multiline)
            res = regex.Replace([text].ToString(), String.Empty)
        End If
        Return res
    End Function
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now