This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
In this article, you will learn how to replace all occurrences of a word in Javascript.
Let’s say you have a string variable named ‘a’ with value “How much wood would a woodchuck chuck if a woodchuck could chuck wood?”.
var a = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
In order to replace all occurrences of a word in a string, you can use the replaceAll()
method. In this example, you will be replacing all occurrences of the word “wood” into “steel”.
var a = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
console.log(a.replaceAll("wood", "steel"));
Note: The replaceAll()
method functions by replacing all occurrences of the first argument with the second argument.
The post How to Replace All Occurrences of A Word in Javascript appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-02-17T12:41:30+00:00) How to Replace All Occurrences of A Word in Javascript. Retrieved from https://www.scien.cx/2021/02/17/how-to-replace-all-occurrences-of-a-word-in-javascript/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.