Something Like that:
replace()
Replacing String Content
The replace() method replaces a specified value with another value in a string
For example I want to written Please visit Home2it! The new Microsoft.
For that you need to create first html code.
HTML
<p id="demo"></p>
After that add this JS code.
JS
str = "Please visit Microsoft! The new Microsoft";
var res = str.replace("Microsoft", "Home2it");
document.getElementById("demo").innerHTML = res;
[codepen_embed height=”265″ theme_id=”0″ slug_hash=”PKqBmL” default_tab=”js,result” user=”pradeepanvi”]See the Pen Replace in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]
Now you can see.
Please visit Home2it! The new Microsoft
But there is one thing, We want to change all Microsoft text to Home2it.
For that we need to use global replace /Microsoft/g
The replace() method can also take a regular expression as the search value.
By default, the replace() function replaces only the first match. To replace all matches, use a regular expression with a g flag (for global match)
For that you need to create first html code.
HTML
<p id="demo"></p>
After that add this JS code.
JS
str = "Please visit Microsoft! The new Microsoft";
var res = str.replace(/Microsoft/g, "Home2it");
document.getElementById("demo").innerHTML = res;
[codepen_embed height=”265″ theme_id=”0″ slug_hash=”gxpjvy” default_tab=”result” user=”pradeepanvi”]See the Pen Global Replace in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]