Mote Interactive

Regular Expressions in Dreamweaver

What are Regular Expressions?

I won't get into the complexities of Regular Expressions, since I'm definitely a novice in the matter, but the gyst is: Regular Expressions are a bunch of strange symbols you use to search inside of a string.

When using Regular Expressions, you're usually Searching and Replacing something. In an example that occured to me today, I had to turn this:

<TD BACKGROUND=""><a href="Behrens.htm?id=TheThreeSisters"><img src="jpegs/The_Three_Sisters_954_RC6300_sm.jpg" alt="The Three Sisters" width="65" height="65" border="2"></a></TD>

into this:

artListArray.push(new Array("The_Three_Sisters_954_RC6300","The Three Sisters"));

A simple search and replace wasn't going to do it because the information after "Behrens.htm?id= changed with each line, and there were hundreds of these lines. What I needed was a search and replace that matched <TD BACKGROUND and a wildcard number of characters until I got to the backslash just before the blue text. So I needed Search and Replace to find and select all the bolded text in this next example:

<TD BACKGROUND=""><a href="Behrens.htm?id=TheThreeSisters"><img src="jpegs/The_Three_Sisters_954_RC6300_sm.jpg" alt="The Three Sisters" width="65" height="65" border="2"></a></TD>

 

Regular Expressions in Dreamweaver

In Dreamweaver, Find and Replace is super convenient. Just hit Ctrl-F(or Apple-F on a Mac) and up pops a simple-to-use palette.

At the bottom of this palette is a Use Regular Expressions radio button, and I haven't totally experimented with it to see how well it matched Perl's Regular Expressions, but it works well with what I needed.

So, to find all the examples of "<TD BACK...jpegs/" and replace them with "artListArray.push(new Array("" I had to enter something like this in the Find field

<TD [A-Za-z0-9 =_<>?".]*[A-Za-z. ="?0-9<>]

and this ends up selecting all the bolded text up to the slash after "jpegs". The asterisk acts like a wildcard looking for whatever is between the square brackets. Between the first set of square brackets I'm looking for all capital letters A-Z, all lower case letters a-z, all numberal 0-9, a space, an equals sign, an underscore, a greater than and lesser than sign, a question mark, a double quote and a period. The second set is the same, all that's missing is the slash and when it hits that it stops selecting.

A simpler example:

<a href="home.htm">Mason</a>,
<a href="index.htm">Masonic</a,
<a href="delta.htm">Delta</a>, and
<a href="omega.htm">Omega</a>.

If I wanted to remove all the links from this page I could get rid of the trailing </a> with a simple Find/Replace, but the first "<a hrefs" are all different.

<a href="[^>]*[>]

Here, I'm using the ^ to negate my search for the >, meaning "everything but >". Then I end my search with [>]. A simpler example, and even my longer example could be shortened to:

<td [^/]*[/]

MORE TUTORIALS