Update to use String.prototype.replace and regular expressions

Removed loop for character checks and string concatenation and replaced with simpler `String.prototype.replace` via regular expressions.