Regular expression - Remove double spaces inside HTML tags

Posted on 2005-05-05
Last Modified: 2012-06-27

I need a regular expression to remove double spaces inside HTML tags.


<font         face="arial">
<font     size    =    2>

I need to run the regexp and get:
<font face="arial">
<font size = 2>

Question by:richardoc
    LVL 13

    Expert Comment

    you may try replace function, or trim

    str = Replace (myStr,"  "," ")

    str = Trim(myStr)

    LVL 10

    Accepted Solution


    s="blah       blah blah  blah" & vbCrLf & _
        "<font         face=""arial"">" & vbCrLf & _
        "<font     size    =    2>" & vbCrLf

    s = RemoveInHTML(s,"[ ]+")

    Response.Write Server.HTMLEncode(s)

    Function RemoveInHTML(str,what)
      Dim r, ms, itm, n
      Set r = New RegExp

      r.Global = True
      r.Pattern = "<(.*?)>"
      Set ms = r.Execute(str)

      r.Pattern = what

      For Each itm In ms
        n=Replace(n, itm.Value, r.Replace(itm.Value," "))

      RemoveInHTML = n
    End Function


    (this may be too much, there might be a simplier way)
    LVL 10

    Expert Comment

    If you use replace(nurbek's comment) ... as long as you loop it, it will kind of work

    r="blah       blah blah  blah" & vbCrLf & _
        "<font         face=""arial"">" & vbCrLf & _
        "<font     size    =    2>" & vbCrLf

    Do While InStr(r,"  ") > 0
      r = Replace(r,"  ", " ")

    This will Also remove the double spaces outside the HTML code also

    I could not tell you wish would be faster or ideal, since I do not know =)

    But good luck.

    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!

    Join & Write a Comment

    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…
    Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
    This video discusses moving either the default database or any database to a new volume.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    754 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

    21 Experts available now in Live!

    Get 1:1 Help Now