Replacing Elements using Regular ExpressionsYou've already seen how useful regular expressions are for finding, matching, and replacing strings. But you can also use them to replace the names of page elements, and this can often save you a bunch of time. In this task, we're going to retrofit a regular expression into a script that you've seen before, Script 4.5. That script built three-state rollovers. It's a useful script, but it has one drawback: it requires you to have tagged every image that you want to manipulate with its own id. That's not too difficult, but you can instead use JavaScript to build the names of page elements and save you some work. At this point, you should review Scripts 4.4 and 4.5 to see what's going on in this example. Go ahead, we'll wait. Back so soon? Great. In short, instead of creating the _click and _on names of an image on the fly based on the id of each image, we're instead creating the _click and _on names on the fly based on the _off name of the image. That way, we don't even need the image ids. Script 8.8 shows you the way. There's no change in the way the page looks or acts from changing the JavaScript; but it saves you work in creating the HTML pages. Script 8.8. Use regular expressions to save you from writing or retrofitting your HTML files.
To use a regular expression to replace an element:
Tip
|