RegEx to strip font tags in ASP

Posted on 2005-04-18
Medium Priority
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
  • 2

Expert Comment

ID: 13829070
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>"))

Accepted Solution

RickDai earned 1400 total points
ID: 13847105
Did this solve your problem? DO you need more help?

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

850 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