- Adding Text
- Cutting, Copying, and Pasting Text
- Dragging and Dropping Text
- Using Paste Special
- Applying Headings
- Applying Character Formats
- Applying HTML Text Styles
- Using Preformatted Text
- Adding Line Breaks
- Indenting Text
- Aligning Text (HTML Method)
- Working with Lists
- Setting List Properties
- Nesting Lists
- Inserting Special Characters
- Adding Automatic Dates
- Adding Horizontal Rules
- Finding and Replacing
- Finding Text with a Simple Search
- Performing Advanced Text Searches
- Finding and Replacing in Source Code
- Finding and Replacing with a Specific Tag
- Using Regular Expressions for Searching
- Checking Spelling
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) 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
CHARACTER |
MATCHES |
\ |
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 |
\b |
Word boundary (such as a space or carriage return) |
\B |
Non-word boundary |
\d |
Any digit 0 through 9 (same as [0-9]) |
\D |
Any non-digit character |
\f |
Form feed |
\n |
Line feed |
\r |
Carriage return |
\s |
Any single white space character (same as [ \f\n\r\t\v]) |
\S |
Any single non-white space character |
\t |
Tab |
\w |
Any letter, number, or the underscore (same as [a-zA-Z0-9_]) |
\W |
Any character other than a letter, number, or underscore |
[abcde] |
A character set that matches any one of the enclosed characters |
[^abcde] |
A complemented or negated character set; one that does not match any of the enclosed characters |
[a-e] |
A character set that matches any one in the range of enclosed characters |
[\b] |
The literal backspace character (different from \b) |
{n} |
Exactly n occurrences of the previous character |
{n,} |
At least n occurrences of the previous character |
{n,m} |
Between n and m occurrences of the previous character |
() |
A grouping, which is also stored for later use |
x|y |
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:
<!--[\w\W]*?-->
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:
Choose Edit > Find and Replace, or press Ctrl-F (Cmd-F).
The Find and Replace dialog appears.
- From the Find in pop-up menu, choose one of the options for the scope of your search.
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.
Enter the regular expression in the Find field (Figure 4.39).
Figure 4.39 You can add regular expressions to both the Find and Replace fields.
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.
- (Optional) Enter text or a regular expression in the Replace field.
- Depending on what you want to do, click Find Next, Find All, Replace, or Replace All.