Optimized replace() and collapseWhitespace() by reusing regexReplace().

Fixed bug in replace() where replacement string was being escaped with preg_quote() and added matching tests.