idsfa,
I am not sure I made the problem clear as your response at first glance isn't appropriate. The task is to change foo to bar in rendered HTML while keeping the original formatting. What it boils down to changing foo to bar in the underlying HTML (which may involve imbedded tags) so that the rendered HTML looks like you did s/foo/bar/g