Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Adobe Dreamweaver

  • Print
  • + Share This
This chapter is from the book

Using Regular Expressions for Searching

A regular expression is a pattern, written using special symbols, which describes one or more text strings. You use regular expressions to match patterns of text, so that Dreamweaver can easily recognize and manipulate that text. Like an arithmetic expression, you create a regular expression by using operators, in this case operators that work on text, rather than numbers.

The operators in a regular expression (see Table 4.5 on page 130) are like the wildcard symbols that you may have seen in find and replace features in other programs, such as word processors, except that regular expressions are much more powerful. They can also be complex and difficult to learn and understand, so if Dreamweaver's other finding and replacing methods are sufficient for you, you may not need to bother with regular expressions.

Table 4.5. Regular Expression Special Characters




Escape character; allows you to search for text containing one of the below special characters by preceding it with the backslash


Beginning of text or a line


End of text or a line


The preceding character zero or more times


The preceding character one or more times


The preceding character zero or one time


Any character except newline


Word boundary (such as a space or carriage return)


Non-word boundary


Any digit 0 through 9 (same as [0-9])


Any non-digit character


Form feed


Line feed


Carriage return


Any single white space character (same as [ \f\n\r\t\v])


Any single non-white space character




Any letter, number, or the underscore (same as [a-zA-Z0-9_])


Any character other than a letter, number, or underscore


A character set that matches any one of the enclosed characters


A complemented or negated character set; one that does not match any of the enclosed characters


A character set that matches any one in the range of enclosed characters


The literal backspace character (different from \b)


Exactly n occurrences of the previous character


At least n occurrences of the previous character


Between n and m occurrences of the previous character


A grouping, which is also stored for later use


Either x or y

Learning regular expressions is beyond the scope of this book, but we'll show you how to use one in an example. Let's say that you want to find all of the HTML comments throughout your site. You can use this simple regular expression:


Let's break that expression down. You read a regular expression from left to right. This one begins by matching the beginning characters of the HTML comment, <!—. The square brackets indicate a range of characters; for example, [a-z] would match any character in the range from a to z. In this case, the range includes two regular expression operators: \w means "any single letter, number, or the underscore," and \W means "any character other than a letter, number, or underscore." Taken together as a range, [\w\W] means "any range of characters." The * means "the preceding character (in this case, everything found by the contents of the square brackets) zero or more times" and the ? means "the preceding character zero or one time." Taken together, they match a comment of any length. The regular expression ends by matching the closing characters of an HTML comment, —>.

To search with a regular expression:

  1. Choose Edit > Find and Replace, or press Ctrl-F (Cmd-F).

    The Find and Replace dialog appears.

  2. From the Find in pop-up menu, choose one of the options for the scope of your search.
  3. From the Search pop-up menu, choose any of the options.

    In this case, since we're looking for HTML comments, you should choose Source Code.

  4. Enter the regular expression in the Find field (Figure 4.45).
    Figure 4.45

    Figure 4.45 You can add regular expressions to both the Find and Replace fields.

  5. Select the check box next to "Use regular expression."

    When you choose "Use regular expression," it disables the "Ignore whitespace" search option, because they are mutually exclusive.

  6. (Optional) Enter text or a regular expression in the Replace field.
  7. Depending on what you want to do, click Find Next, Find All, Replace, or Replace All.
  • + Share This
  • 🔖 Save To Your Account