This is a quickie. I recently had a lengthy form using the <label> tag, and needed to switch label methods. Being industriously lazy, I figured a Regular Expression search and replace would do the trick. Fortunately DreamWeaver CS4 supports Regular Expressions in its Search and Replace dialog box (as long as you turn the appropriate checkbox on!).
In case you’re not too familiar with the <label> tag, it can be used in two ways:
<label>First Name<input type="text" name="firstName" /></label>
and
<label for="firstname">First Name</label><input type="text" name="firstName" />
They look similar, but the salient difference here is that one form of the <label> tag usage encapsulates the form element, while the other does not. There are various (mostly) CSS-related reasons why you would choose one over the other, most notably in terms of whether you would rather be able to have a block surround your text label and input form, or whether you would rather be able to have two separate blocks (which might need to be encapsulated by further markup – such as an ordered list item, as suggested on A List Apart.
Anyway, I was using the first form, and needed to switch to the second form of the tag. The following two regular expressions will do that for you:
“Find” RegExp: <label>([^<]+)(<input.+name=”([^"]+)” \/>)<\/label>
“Replace” RegExp: <label for=”$3″>$1</label>$2
What’s cool here is that it automatically populates the “for” attribute of the label with the “name” attribute of the input tag, while leaving the rest in place.
Big Disclaimer: There is a good chance that this RegExp will not work for your specific situation, because it is highly specific. If you are not familiar with Perl-style Regular Expressions, you will most likely not be able to make much use of this unless your input tags are exactly the same as mine (ie: the “name” attribute is the last attribute before the closing />, all your spacing is the same, everything is lower case etc). If you would like me to craft a more generic RegExp, leave a comment and I will do one up.