This is probably a Java String vs Javascript String problem. Java Strings have a replaceAll() method, but Javascript Strings do not.
You can force/cast your string so it is a Javascript string and then do a global replace:
str = '' + str;
str.replace(/foo/g, "bar");
OR
You can force/cast your string so it is a Java String and then do a replaceAll:
str = new java.lang.String(str);
str.replaceAll("foo", "bar");