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.


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


Print Share Comment Cite Upload Translate Updates
APA

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/

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 ,<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 ]. 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 . 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: ]
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/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.