Adam Howitt's Blog

Feb 25
2005

Simplifying Form Validation with Eclipse Backreferences

I'm in awe of Eclipse and the productivity it affords me. Today's revelation comes as I look at a form with 43 fields defined by our graphics guy and a need to set the value="" attribute to the previously submitted value if it exists. With Eclipse and backreferences in the find/replace I was able to do it in one shot.

All my input fields were defined as
<input name="fldname" ... blah, blah, blah >

  • Take a copy of your code in case this blows up on you
  • Ctrl+F opens the find / replace window. Set the find string to
    name="(.*?)"
    This finds the name="" attribute and whatever is between the quotes is stored as a backreference number 1
  • Set the replace string to
    name="$1" value="#attributes.$1#"
    This says we are going to output two attributes in place of the one and for the value we are adding some CF code around the backreference $1 also.
  • Check the regular expressions box
  • Click find to be sure that your regex finds a good first match.
  • Click replace to replace just one occurence to check that it outputs in the way you want (it's easier to undo one mistake vs 43)
  • If all is good hit replace all

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
[Add Comment] [Subscribe to Comments]
  1. Yeah, regular expression finds are a great time savers, but they aren't limited to just Eclipse, now are they, Adam? ;)

    Dreamweaver can do the same find and replace within its F&R dialog. Just check the "Use regular espresion" and away you go.

  2. To be fair, I didn't say Dreamweaver or Homesite couldn't either... You Dreamweaver guys are so defensive ;-)

    Out of interest, what is the backreference identifier in Dreamweaver and Homesite?

  3. $, just like in eclipse. Can't say with 100% certainty, but I believe the identical expressions would work in DW (among many other editors with RE support).

  4. Yeah, but to be fair back at you, starting off with "I'm in awe of Eclipse and the productivity it affords me." Does imply that other dev platforms you use don't.

    Dreamweaver uses JavaScript 1.5 regular expressions (at least that's what "they" say they do), so they should be pretty much the same common coding as are available in other apps/languages.

    The exact find and repalce that you specified works in Dreamweaver. However, I probably wouldn't use that one because if you have other elements with names, not just input tags, like say named anchors, you'll get incorrect results.

    I've personally used $1-$3 in searches, but would assume that they continue up through $9

    As for HomeSite, I'm not certain, as I really don't use it, but I'd assume that it would be the same.

  5. Not to be fair at all to anyone, but I've seen posts and follow-ups/comments like this before. Let's just set the record straight once and for all. NOT including something does not imply exclusion. It just means Adam was making a post about his preferred tool (for now). If I were to post something about color correction I'd specifically point to Photoshop and give a point by point reference. That doesn't mean there are NO other software tools in the world that do color correction - it just means that's what I'm using/am familiar with.

  6. FWIW, the backreference in HS+ is \ (as in \1, \2, \3, etc.). I don't know whether the $ would work, but I'm sure of the other one (having just done a bunch of Regex hacking the other night, to get some XML tags into a bunch of sorta-formatted text).

    Cheers, REM O-

[Add Comment]