Finding a substring and inserting another string
var a = "xxxxhelloxxxxhelloxxxx";
a = a.replace(/hello/g,"hello world"); // if you want all the "hello"'s in the string to be replaced
document.getElementById("regex").textContent = a;
a = "xxxxhelloxxxxhelloxxxx";
a = a.replace("hello","hello world"); // if you want only the first occurrence of "hello" to be replaced
document.getElementById("string").textContent = a;
<p>With regex: <strong id="regex"></strong></p>
<p>With string: <strong id="string"></strong></p>
This will replace the first occurrence
a = a.replace("hello", "helloworld");
If you need to replace all of the occurrences, you'll need a regular expression. (The g
flag at the end means "global", so it will find all occurences.)
a = a.replace(/hello/g, "helloworld");