Implement replace/3

This currently uses a naïve string matching algorithm.