Look at trim in side DDG::Util::String. It already has logic to trim avoiding italics. I haven't followed this too closely so ignore me if this is not relevant, but it seems relevant here.

What does does is compress <i> (in that case) to a one char utf char, then does the trim and and correct if it it lopped off something at the end. Seems much simpler.