Replacing Elements using Regular Expressions

You’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 let JavaScript build the names of page elements and save yourself some work.

At this point, you should review Chapter 4’s image rollovers (Scripts 4.4 and 4.5) to ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.