in reply to preserving html in patterns

I like Jeffa's answer, because the substitution pattern is easier to read.

Alternatively, I think you could get away with just adding the 'e' modifier after your 'g'. That tells s/// to evaluate the right side, rather than use it at face value.