How to remove span tags from html in

Posted on 2006-06-06
Last Modified: 2008-01-09
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?
Question by:tegronakron
    LVL 4

    Expert Comment

    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;

    Author Comment

    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

    LVL 4

    Accepted Solution

    Try this "<[^>]*>"

    Author Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    Ajax and MVC 2 52
    'Oracle.ManagedDataAccess.Client' could not be loaded 4 68
    jquery, aspx 10 33
    How to perform calculation 2 19
    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now