In similar situations I normally use the ReReplacer extension (https://www.regularlabs.com/extensions/rereplacer) by Peter van Westen.
With it you can essentially do what Johan suggested (regex the
content of the output), but with a gazzilion of useful
options. I never found it to be particularly "heavy", with very
good page preparation times. I suggest you to try the free version
and if it fits your needs eventually upgrade to the paid-for
version (compare the features in the above linked page)