The Regex Library

Valid HTML

A: Adding Quotes to Attributes

Search for: =([^"][^ >]*)
Replace with: ="\1"

I use the two parts of the string above in BBEdit when I'm cleaning up my old code. It searches for equal signs, followed by anything other than a quotation mark, followed by as many characters as there are until it reaches a space or a "greater than" sign. Then the replacment section changes the attribute=value form to attribute="value".

This string came about because of lazy coding in the wilds of my youth. (Okay, a couple years ago.) I would often have something along the lines of <P ALIGN=RIGHT> in my code. Only when an atribute had a space in it's value did I use quotes: <INPUT VALUE="This is a sample">. Wanting to make my code conform better to the standards of HTML, I developed this pattern to clean up these troublesome quotes and attributes. Ah, the joys of hand coding HTML.

This Regex string isn't perfect. The worst part is if you use equal signs as 'ascii art' or if you have a URL that has an equal sign in it. I use this in a interactive mode on each page as I update them, but this probably won't work on a blind multi-file search and replace.

This site hand-coded for your protection.