I have a site with about 300 pages that have the same title. I need to replace that title with the same text that appears in the h1 tag on the page (each page is different.) Can DW's Find and Replace do this? If so, how? I really don't want to go have to go into each page and copy/paste.
OK, I've done a quick test on one page, and this works. But before attempting to use this solution, BACK UP YOUR SITE.
You need to use the following regular expression (regex) in the Find field:
<title>([^<]+)(</title>[\S\s]+)<h1>([^<]+)
See corrected version in next post
In the Replace field:
<title>$3$2$3
Select the "Use regular expression" checkbox.
Test it on a couple of pages. Then take a deep breath, and do it on the entire site.
Thanks, David, but I'm not getting it to work. All that happens when I click Replace is that the code between and including the title and h1 tags is highlighted. Nothing gets changed.
I see that you have "This is the Title I want" next to "Find in Current Document". Should I be seeing something there in mine, because I don't. Does that appear after the replace is complete?
jeffsure wrote:
Is there a way to tell it to place only the first h1 tag into the title?
I am really motivated now to learn regex!
Yes. Just use the following regex instead of the previous one:
<title>([^<]+)(</title>[\S\s]+?<h1>)([^<]+)
The Replace field is the same.
The question mark makes the [\S\s]+ a lazy search, stopping at the first <h1> rather than doing a greedy search which finds the last <h1> in the page.
Learning regex is tough, but it can be really rewarding. I wish I knew them better.
Hi David,
Thanks for your posts in this thread. I was able to get your Regex code to work, too, but I need a slightly different code change. I need to change the title tag sitewide, but instead of matching the h1 tag, it has to match an id tag.
e.g:
<div id="detailTitle" class="detailText">
Image Title </div>
What's the required tweak to your code?
North America
Europe, Middle East and Africa
Asia Pacific