How to Replace All Occurrences of A Word in Javascript

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’…

The post How to Replace All Occurrences of A Word in Javascript appeared first on CodeSource.io.

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.


Print Share Comment Cite Upload Translate
APA
Ariessa Norramli | Sciencx (2024-03-29T14:45:59+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/.
MLA
" » How to Replace All Occurrences of A Word in Javascript." Ariessa Norramli | Sciencx - Wednesday February 17, 2021, https://www.scien.cx/2021/02/17/how-to-replace-all-occurrences-of-a-word-in-javascript/
HARVARD
Ariessa Norramli | Sciencx Wednesday February 17, 2021 » How to Replace All Occurrences of A Word in Javascript., viewed 2024-03-29T14:45:59+00:00,<https://www.scien.cx/2021/02/17/how-to-replace-all-occurrences-of-a-word-in-javascript/>
VANCOUVER
Ariessa Norramli | Sciencx - » How to Replace All Occurrences of A Word in Javascript. [Internet]. [Accessed 2024-03-29T14:45:59+00:00]. Available from: https://www.scien.cx/2021/02/17/how-to-replace-all-occurrences-of-a-word-in-javascript/
CHICAGO
" » How to Replace All Occurrences of A Word in Javascript." Ariessa Norramli | Sciencx - Accessed 2024-03-29T14:45:59+00:00. https://www.scien.cx/2021/02/17/how-to-replace-all-occurrences-of-a-word-in-javascript/
IEEE
" » How to Replace All Occurrences of A Word in Javascript." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/02/17/how-to-replace-all-occurrences-of-a-word-in-javascript/. [Accessed: 2024-03-29T14:45:59+00:00]
rf:citation
» How to Replace All Occurrences of A Word in Javascript | Ariessa Norramli | Sciencx | https://www.scien.cx/2021/02/17/how-to-replace-all-occurrences-of-a-word-in-javascript/ | 2024-03-29T14:45:59+00:00
https://github.com/addpipe/simple-recorderjs-demo