19. Add Proper HTML <TITLE> Tags
Many web authors are sloppy at adding proper <TITLE> tags to their HTML files. They are easy to forget
because they are not clearly visible when viewing a website. However, <TITLE> tags are important because
they’re used as the default name for bookmarks/favorites. Most search engines will use the titles to list your
pages in the search results.
Assuming you have been more careful with adding the title to the HTML body, you can easily fix this
problem with PowerGREP. Usually, <H1> tags are used to add titles to the body. We will use <H1> tags in
the example below, but you can easily adapt it to whatever tags you have been using. What we’ll do is tell
PowerGREP to find the <H1> tag in each file and capture its contents. Then we use the captured text to
replace the <TITLE> tag.
The ´filter filesµ feature on the Action panel is what we’ll use to capture the <H1> tag into a named
capturing group. Then we can set the main action to search for the <TITLE> tag and to replacde it with the
contents of the named capturing group. This relies on PowerGREP’s special ability to carry over text
matched by named capturing groups from one part of the action to the next.
Select the HTML files you want to update in the File Selector.
Start with a fresh action.
Set the action type to ´search-and-replaceµ.
Set ´filter filesµ to ´require all search terms to matchµ. Leave the search type as ´regular expressionµ.
Enter the regular expression «
» in the Search box in the ´filter filesµ part of
the action. This regex matches the opening and closing <h1> tags and any text between them. The
text between them is captured into the named capturing group ´h1µ.
Enter the regular expression «
» in the Search box in the main part of the
action. This regex matches the opening and closing <title> tags and any text between them. This
regex does not capture anything.
Enter the replacement text ´
µ to insert a new pair of title tags with the text
matched by the named capturing group ´h1µ between them.
Set the target and backup file options as you like them.
Click the Preview button to run a test.
If all looks well, click the Replace button to actually update the TITLE tags.
Should a file not have an <H1> tag, then it is filtered out and no changes are made to it. If a file has more
than one <H1> tag, then only the first tag is used. Once all the regular expressions in ´filter filesµ have found
a match, PowerGREP considers the file to meet the filtering requirement. It won’t look for any further
matches for the filtering regex.
If a file does not have an <TITLE> tag, the search-and-replace won’t replace anything. If a file has more
than one <TITLE> tag, then all of them are replaced with the contents of the first <H1> tag in the file.
This action is available in the PowerGREP.pgl library as ´Update HTML title tagsµ.
How to Insert Missing <TITLE> Tags
If some of your HTML files do not have TITLE tags at all, but they do all have <HEAD> tags, you can use
the following regular expression «
» for the search-and-replace.