Finding and Replacing Text

In its simplest form, the Find and Replace feature allows you to search for text by entering the text you're looking for and clicking the Find button. The real power comes into play when you are doing complex searches and searching inside of HTML code.

graphics/troubleshooting_icon.jpg

If you are searching for text in HTML code and you are unable to click the Find in Source Code check box, see "Unable to Select Find in Source Code Radio Button" in the "Troubleshooting" section of this chapter.


Suppose that you have just created a new Web site for a client. The Web site contains a couple of hundred pages, and each page has several telephone numbers listed as static text. The phone numbers are entered in the format (###)-###-####. Your client is very particular about the site and wants all phone numbers changed to the format ###.###.####.

In previous versions of FrontPage, you'd be stuck with hours of tedious work to change all these entries. With the new Find and Replace feature, the task is quick and easy because for the first time in FrontPage, you can use regular expressions to find and replace text.

NOTE

Regular expressions are special codes that allow for detailed control over text searches and replacements. For a great reference on regular expressions, read Sams Teach Yourself Regular Expressions in 10 Minutes.


Developing Regular Expressions for Find and Replace

To access the Find and Replace dialog box, open your Web site in FrontPage and select Edit, Find to open the dialog box with the Find tab selected, or Edit, Replace to open the dialog box with the Replace tab selected.

The first thing you will need to do is develop a regular expressions statement that will locate all instances of the incorrectly formatted phone number. You can build regular expressions from either the Find tab or the Replace tab. When building a regular expression that will replace text in your Web pages, it's often a good idea to build it in the Replace tab so that you can easily reference both the Find and the Replace regular expression. However, if you do build it in the Find tab and then click the Replace tab, your regular expression will still exist inside the Find What text box.

In the phone number example, you need to find one digit between 1 and 9, followed by two digits between 0 and 9. These three digits should be enclosed in parenthesis and followed by a dash and three digits between 0 and 9. To complete the phone number, you will find three more digits, followed by another dash and four digits between 0 and 9. Here's the regular expression that will match your search:

 
 \({[1-9][0-9]^2}\)\-{[0-9]^3}\-{[0-9]^4} 

Table 5.1 lists the regular expression characters used in the phone number regular expression.

Table 5.1. Regular Expression Characters

Regular Expression Character

Purpose

\

The escape character. Specifies that the character immediately following is part of the text to find and not part of the regular expression syntax.

[]

Square brackets indicate that any character within the brackets will produce a match.

{}

Marks the enclosed expression as a tagged expression that can be reused later.

^n

Repeat expression character. Indicates that the expression preceding it should be repeated n times.

The first character you want to find is the open parenthesis. The open parenthesis is represented in the regular expression as \(. Because parentheses are special characters that are actually used in regular expressions, you want to make sure that FrontPage knows that you are literally looking for the parenthesis character and not simply using the parenthesis to enclose a part of the regular expression. To indicate that a character is part of the text you want to find and not simply part of the regular expression itself, you precede the character with a backslash. This is known as escaping the character, and the backslash character is known as the escape character.

The first character in the area code must be from 1 to 9, so the next part of the regular expression is {[1-9]. When characters are enclosed in square brackets, it means that if any of those characters are found, it will produce a match. The opening curly bracket will be explained later.

To finish the area code portion of your regular expression, you want to search for a set of two numerals, both from 1 to 9, followed by a closing parenthesis. The regular expression syntax for this search string is [1-9]^2\)}. The ^ character is the repeat expression character, and followed by a 2, it means that the [1-9] expression should be repeated two times. Finally, the closing parenthesis is escaped with the backslash. Once again, I'll explain the curly brackets shortly, so ignore them for now.

Next you need to search for a dash, followed by three characters of any digit from 0 to 9, followed by another dash. The regular expression for this is \-{[0-9]^3}\-. The dash, just as the parentheses, is a special character that must be escaped with a backslash.

The final search is for four characters from 0 to 9. The regular expression for this is {[0-9]^4}. You now have a regular expression that will find any phone number in the format (###)-###-####.

If you can't remember what all the regular expressions characters are, the Find tab provides a regular expressions menu that allows you to insert them easily and provides a description of each (see Figure 5.2). To access the regular expressions menu, click the top arrow button to the right of the Find What text box.

Figure 5.2. The regular expressions menu in the Find tab makes creating regular expressions easier.

graphics/05fig02.jpg

Notice that each of the three sets of numbers in the regular expression are each enclosed in curly brackets. Expressions enclosed in curly brackets are called tagged expressions. Any text that is matched by a tagged expression can be reused in your replace expression. For example, in the phone number find and replace operation, the parentheses and dashes in the phone number will be removed or replaced, but the phone number itself will remain the same. In order to use the phone number that was found with the find expression, you will store it temporarily in tagged expressions and then use those tagged expressions to insert the phone number into your replace expression.

Your replace regular expression will replace any phone numbers that are found with the same phone number in the format ###.###.####. Here is the syntax for that regular expression:

 
 \1\.\2\.\3 

The first two characters, \1, represent the first tagged expression. This means that the characters returned by the search string that was within the first set of curly brackets will be inserted in place of the \1. Inserting tagged expressions is made easy with the regular expressions menu in the Replace dialog box (see Figure 5.3), which is accessible by clicking the top arrow button next to the Replace With text box.

Figure 5.3. The regular expressions menu in the Replace dialog box allows you to easily use regular expressions to replace text.

graphics/05fig03.jpg

Next a period is inserted, and because a period is a special character, it is escaped with a backslash. This is followed by the second tagged expression, another period, and the third tagged expression. You can now run the search and locate and replace your incorrectly formatted phone numbers.

In this example, you'll likely want to search through all files in the site. To do that, select the All Pages radio button in the Find Where section of the Find and Replace dialog. After selecting that, you'll notice that a Find in Site button appears. By clicking this button, FrontPage will search through all files in your site and will display all files with one or more matching results in the bottom of the dialog as shown in Figure 5.4.

Figure 5.4. The Find in Site feature makes it easy to make replacements in multiple files.

graphics/05fig04.jpg

Double-clicking a file in the list will open that file and take you to the first occurrence of the search string. After you've made your change, click the Return to List button in the Find and Replace dialog to return to the list of files. The red dot changes to a yellow dot, and the file's status is marked as Edited as seen in Figure 5.4.

If a Find and Replace operation changes text on a page that is currently opened in FrontPage, the changes will not be automatically saved. However, if a Find and Replace operation is performed against a page that is not opened in FrontPage, any changes will be automatically saved by FrontPage and cannot be undone.

HTML Rules

HTML Rules add another level of power to the Find and Replace feature. Suppose your requirements in the previous phone number example are that you replace the phone number format everywhere in the Web site except when the phone number appears inside of a <div> tag. In cases where the phone number is located inside of a <div> tag, you want to leave it alone. HTML Rules allow you to accomplish this easily.

Enter the search expression in the Find What text area and the replace expression in the Replace With text area as seen in Figure 5.5.

Figure 5.5. The Find and Replace dialog with the regular expressions ready for your phone number replacement.

graphics/05fig05.jpg

Click the HTML Rules button to open the HTML Rules dialog. Click New Rule and select Not Inside Tag from the dropdown. Another dropdown will appear with a list of HTML tags. Select div from the second dropdown (see Figure 5.6) and click OK. The Find and Replace dialog should now indicate that an HTML rule is in effect. Now when you run a replace, FrontPage will only replace those phone numbers that are not inside a <div> tag.

Figure 5.6. HTML Rules are an extremely powerful tool for searching and replacing in a Web site.

graphics/05fig06.jpg

You can take HTML Rules as far as you want to go, and with each level, more options are available to you. For example, you can easily search for all instances of text inside an <a> tag not containing a target attribute.

graphics/troubleshooting_icon.jpg

If you are having touble with FrontPage not finding your search text inside an HTML tag, see "Expression Not Found When Searching HTML" in the "Troubleshooting" section of this chapter.




Special Edition Using Microsoft Office FrontPage 2003
Special Edition Using Microsoft Office FrontPage 2003
ISBN: 0789729547
EAN: 2147483647
Year: 2003
Pages: 443

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net