Search code examples
htmlhref

How do I replace one character with another but ONLY in href?


I have hundreds of links in below format:

a href="../Lorem_ipsum__"  title="Dolor_sit amet" target="__blank"

I need to change the _ (underscore) character to - (hyphen), but ONLY in href (title and target need to remain unchanged).

I tried to do it with Dreamweaver's 'Find and Replace', but didn't succeed.


Solution

  • Tick "Use regular expression" in Find and Replace, then search for:

    (href="[^_"]*)_+([^"]*)
    

    And replace it with:

    $1-$2
    

    The first parenthesis group searches for anything that starts with href=", then all characters except _ or ".

    Then we have the _ one or more times.

    The second parenthesis group searches for anything and stops before the closing "

    You can then use the groups with $1 and $2 in Replace.

    Here's an interactive tutorial if you want to learn regular expressions (regex): http://regexone.com/