Skip navigation
Currently Being Moderated

How can I use Find and Replace to replace title of doc with copy of h1 content?

Jul 4, 2012 10:33 AM

Tags: #find #replace #dreamweaver_cs4

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.

 
Replies
  • Currently Being Moderated
    Jul 4, 2012 10:38 AM   in reply to jeffsure

    No, DW's F&R cannot do such a thing.  I'm afraid you're doomed to manual updating.  Get a tall-neck beer, and nut it out!

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 5, 2012 5:27 AM   in reply to MurraySummers

    Actually, I think it probably can be automated. This is an interesting challenge. Give me a little while to try to work out how it can be done.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 5, 2012 5:49 AM   in reply to jeffsure

    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.

     

    findandreplace.jpg

    Test it on a couple of pages. Then take a deep breath, and do it on the entire site.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 5, 2012 5:47 AM   in reply to David_Powers

    Oops, there's a typo in that solution.

     

    The regex should look like this:

     

    <title>([^<]+)(</title>[\S\s]+<h1>)([^<]+)

     

    findandreplace.jpg

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 5, 2012 7:43 AM   in reply to David_Powers

    Show off!

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 5, 2012 8:28 AM   in reply to jeffsure

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2013 9:39 AM   in reply to David_Powers

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2013 11:51 AM   in reply to FXCANADA

    If it's the text inside a div with a specific ID, you don't need a regex. Dreamweaver's Find and Replace will do it for you with the following settings:

     

    findandreplace.jpg

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points