String.prototype.replace partial reimplementation.

Credit to https://gist.github.com/jdalton/93632ea7a13c3c83c06e for how to do it. Thanks, @jdalton!