Your problem is not searching inside HTML links, but replacing inside what you already replaced in a previous loop. So: don't do multiple loops, instead, replace everything in one go. Build a regex with all search terms first, and do the substitution with a hash. Something like this:
$_ = <<'--'; Oh no, Anti-globalization activists are coming! Globalization is rejected by... -- use Regex::PreSuf; %links = ( 'globalization' => '/encyclopedia/Globalization/index.html', 'anti-globalization' => '/encyclopedia/Anti-Globalization/index.htm +l' ); my $re = presuf(keys %links); s/($re)/<a href="$links{lc $1}">$1<\/a>/gio; print;
Result:
Oh no, <a href="/encyclopedia/Anti-Globalization/index.html">Anti-glob +alization</a> activists are coming! <a href="/encyclopedia/Globalization/index.html">Globalization</a> is +rejected by...
Regex::PreSuf is a module to build a regex out of a list of words. It'll escape metacharacters, so the resulting regex will always just do literal lookups.

Do note how I made the replacement case-insensitive, making the keys of the hash lower case, and doing the lookup with lc $1 — in addition to use of the /i switch.


In reply to Re: Searching for text not inside a hyperlink by bart
in thread Searching for text not inside a hyperlink by aemain

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.