Make Path.prototype.replace more forgiving of array modifications.

This logic used to be part of the replace method, and I thought I could
take it out, but I was wrong.