Do you have to operate on the string? Can't you convert it to a DOM first?