Is there a way to replace something like this:
testHoraciotestHellotest
And replace the 'test' word via javascript, I've tried with the .replace()
function but it didn't work
Is there a way to replace something like this:
testHoraciotestHellotest
And replace the 'test' word via javascript, I've tried with the .replace()
function but it didn't work
It's simple.
var str = "testHoraciotestHellotest";
var res = str.replace("test", "test1");
console.log(res);
If you want to replace all occurances of 'test' rather than just one, use the following trick instead:
var res = str.split("test").join ("test1");
str.replace()
also accepts regular expressions, so you can use /test/g
to match all instances of 'test' in the string.
var str = "testHoraciotestHellotest";
var res = str.replace(/test/g, "replaced");
console.log(res);
use g to make a global replace to the string
var str = "testHoraciotestHellotest";
var res = str.replace(/test/g, "word");
console.log(res);