in reply to Replace characters not in html tags

To match $a only in data not enclosed in tags you need a regex of
/>[^<]*$a/s;
to replace $a with $b only in data not enclosed in tags you need
s/(>[^<]*)$a/$1$b/sg;