RegEx to strip font tags in ASP

Posted on 2005-04-18
Last Modified: 2013-12-03
Hi Experts,

I have a CMS that allows users to update web content stored in a database. I would like to use a regular expression to clean the content of font tags prior to updaing the database. If a font tag contains a color definition, that part can stay, everything else (font name, size, wieght, etc) needs to be stripped out. If a font tag contains NO color definition then the entire tag (opening and closing) should be removed.

I found a couple website examples that claim to do nearly what I need - but, ahem, I don't know how to use the examples in an asp page. My page has a strNewContent. I already use Replace to to fix single and double quotes before makeing the database update. Could someone please tell me the exact syntax to use a regular expression to clean the font tags from strNewContent prior to updating the database?

Below are the samples I've found:

One website said the following wold remove all font tags when replaced with backreference \6
      <(FONT|font)([ ]([a-zA-Z]+)=("|')[^"\']+("|'))*[^>]+>([^<]+)(</FONT>|</font>)

Another website said the following would strip all but the color from a font tag:
<\*?font              # Match start of Font Tag
  (?(?=[^>]+color.*>)    #IF/THEN lookahead color in tag
     (.*?color\s*?[=|:]\s*?) # IF found THEN move ahead  
        ('+\#*?[\w\s]*'+         # CAPTURE ColorName/Hex
        |"+\#*?[\w\s]*"+             # single or double
        |\#*\w*\b)                   # or no quotes      
        .*?>                     # & move to end of tag
     |.*?>                 # ELSE move to end of Tag
   )                     # Close the If/Then lookahead

# Use Multiline and IgnoreCase

# Replace the matches from RE with MatchEvaluator below:
#   if m.Groups(1).Value<>"" then
#         Return "<font color=" & m.Groups(1).Value & ">"
#   else
#         Return "<font>"
#   end if

Thanks for your help!

Question by:Verdend
    LVL 2

    Expert Comment

    Try this:

    Function StripFontTags(Fontstring)
          Dim RegularExpressionObject
          Set RegularExpressionObject = New RegExp

          With RegularExpressionObject
          .Pattern = "\s+(?!color)(\w)*\s*=\s*(\""[^\""]*\""|\w*)"
          .IgnoreCase = True
          .Global = True
          End With
          If Fontstring <> "" Then
                StripFontTags = RegularExpressionObject.Replace(Fontstring, "")
                StripFontTags = ""
          End If
          Set RegularExpressionObject = Nothing
    End Function

    Response.Write(StripFontTags("<font color=""Red"" size=""3"">asdf</font>"))
    LVL 2

    Accepted Solution

    Did this solve your problem? DO you need more help?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    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!

    I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    779 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

    12 Experts available now in Live!

    Get 1:1 Help Now