in reply to Highlight your result

Your requirement is to only apply your substitution when you are not in a tag. Knowing when you are not in a tag is non-trivial.

You might try HTML::Parser. Your start and end handlers can just write what they read, and have your substitution take place in the text handler.