Because
#1
Your regular expression is now ( example with 0 [0]
); This will match <space>0<space/>
You're probably looking for " \\["+i+"\\] "
as brackets are a special character,
#2
You're not storing the result of the replace, you should do:
a = a.replace(new RegExp(" \\["+i+"\\] ", 'g'),'new');
Comebine that, and you get
var a='some thing [0] [1] [0] [1] ';
for(var i=0; i<3; i++){
a = a.replace(new RegExp(" \\["+i+"\\] ", 'g'),'new');
}
alert(a);
Which outputs (Fiddle) some thingnewnewnewnewnew
and I hope that is the expected result.
Last but not least this is an abuse of regular expressions, and you can easily have a one line solution.