title: Build regex to find and replace invalid HTML attributes
body: <p>The sad truth about this post is that I have poor regex skills.  I recently came across some code in an old project that I seriously want to do something about.  Here it is:</p>
  
  <pre><code>strDocument = strDocument.Replace("font size=""1""", "font size=0.2")
  strDocument = strDocument.Replace("font size='1'", "font size=0.2")
  strDocument = strDocument.Replace("font size=1", "font size=0.2")
  strDocument = strDocument.Replace("font size=""2""", "font size=1.5")
  strDocument = strDocument.Replace("font size='2'", "font size=1.5")
  strDocument = strDocument.Replace("font size=2", "font size=1.5")
  strDocument = strDocument.Replace("font size=3", "font size=2")
  strDocument = strDocument.Replace("font size=""3""", "font size=2")
  strDocument = strDocument.Replace("font size='3'", "font size=2")
  </code></pre>
  
  <p>I'm guessing there is some easy regex pattern out there that I could use to find different ways of quoting attribute values and replace them with valid syntax.  For example if somebody wrote some HTML that looks like:</p>
  
  <pre><code>&lt;tag attribute1=value attribute2='value' /&gt;
  </code></pre>
  
  <p>I'd like to be able to easily clean that tag so that it ends up looking like</p>
  
  <pre><code>&lt;tag attribute1="value" attribute2="value" /&gt;
  </code></pre>
  
  <p>The web application I'm working with is 10 years old and there are several thousand validation errors because of missing quotes and tons of other garbage, so if anybody could help me out that would be great!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I gave it a whirl (found some examples), and have something that will work, but would like it to be a little smarter:</p>
  
  <pre><code>Dim input As String = "&lt;tag attribute=value attribute='value' attribute=""value"" /&gt;"
  Dim test As String = "attribute=(?:(['""])(?&lt;attribute&gt;(?:(?!\1).)*)\1|(?&lt;attribute&gt;\S+))"
  Dim result As String = Regex.Replace(input, test, "attribute=""$2""")
  </code></pre>
  
  <p>This outputs <code>result</code> correctly as:</p>
  
  <pre><code>&lt;tag attribute="value" attribute="value" attribute="value" /&gt;
  </code></pre>
  
  <p>Is there a way I could change (and simplify!) this up a bit so that I could make it look for <em>any</em> attribute name?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Here's what I have so far based on the comments.  Perhaps it could be improved even more:</p>
  
  <pre><code>Dim input As String = "&lt;tag border=2 style='display: none' width=""100%"" /&gt;"
  Dim test As String = "\s*=\s*(?:(['""])(?&lt;g1&gt;(?:(?!\1).)*)\1|(?&lt;g1&gt;\S+))"
  Dim result As String = Regex.Replace(input, test, "=""$2""")
  </code></pre>
  
  <p>which produces:</p>
  
  <pre><code>&lt;tag border="2" style="display: none" width="100%" /&gt;
  </code></pre>
  
  <p>Any further suggestions? Otherwise I think I answered my own question, with your help of course.</p>