Replace Method in JavaScript
First we will need to find this particular variable, Bob, in our biography. Before we can do so, we will need to take the biography and convert it to a string so we can work with it. We will use the document.body.innerHTML function and set it to a variable that will contain our bio like so:
We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud hosting!
var s = document.body.innerHTML;
Now with that taken care of we can begin our search. In order to search for our variable “w” and not quit after the first find, we will need to use a Regular Expression, RegEx, to continue the search. Let’s establish a new variable that contains this expression.
We stand behind Server Intellect and their support team. They offer dedicated servers, and they are now offering cloud hosting!
var w = "Bob";var RegEx = new RegEx(w, 'g');
Next what we will need to do is replace the faulty Bob with our new and improved Bobb and set it back to the Body of our site. The replace function is built so you first establish what you are looking for and then the replacement, separated by a comma. This will look like so:
s = document.body.innerHTML.replace(RegEx, ("Bobb"));
We chose Server Intellect for its cloud hosting, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
ReplaceJavaScriptTut.zip