In this article, we are going to learn how to replace all string in Javascript using the built-in split and join method.
Let's create a prototype to existing String object in Javascript and extend a new method called replaceAll. Here is our code in Javascript.
String.prototype.replaceAll = function (target, replacement) { return this.split(target).join(replacement); }
Demo
To see how it works, you can check it on the following demo in Codepen.
https://codepen.io/bytutorial/pen/ZEELzVm