Sometimes, we want to replace words in the body text with JavaScript.
In this article, we’ll look at how to replace words in the body text with JavaScript.
How to replace words in the body text with JavaScript?
To replace words in the body text with JavaScript, we call the string replace
method.
For instance, we write
document.body.innerHTML = document.body.innerHTML.replace(/hello/g, "hi");
to call replace
to return a string that replaces all instances of 'hello'
with 'hi'
.
And then we set its document.body.innerHTML
to the returned string.
Conclusion
To replace words in the body text with JavaScript, we call the string replace
method.