IMG-LOGO

How to replace all string in Javascript?

andy - 21 Oct, 2019 2250 Views 0 Comment

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

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles