I have a string like this:
var str = "A A A A A";
how do I replace a specific A with something else?
Eg: replace 3rd A to:
var str = "A A 00 A A";
Of the 1st, 2nd, etc.. ?
I have a string like this:
var str = "A A A A A";
how do I replace a specific A with something else?
Eg: replace 3rd A to:
var str = "A A 00 A A";
Of the 1st, 2nd, etc.. ?
I would split the str, replace whatever index, then join back
var str = "A A A A A";
var sp=str.split(' ');
var ind=2;
sp[ind]='00';
console.log(sp.join(' '));
I prefer to use regular expression, here is the answer:
newValue = str.replace(/(([^A]*A){2}[^A]*)A/g, '$100');
Notes: {2} is the number of the occurrence we want to replace.
$100 here we specify which is the new string we want to place after the $1