Hi,
noe, du kommst um einen »callback« nicht herum ...
na gut, meine Variante:
// search for somthing with
// chars, followed by numbers, followed by chars again...
// and replace the numbers
//
var regex = /^([a-z]+)([0-9]+)([a-z]+)$/i;
var str = "Andre23xxl";
// rebuild string like this: match1, replace, match3
//
var rebuild = [1,-1,3];
function sx_replacer(str, repl, regex, rebuild) {
// callback
// arguments: matched string, match1, match2..., offset, string
//
return str.replace(regex, function(){
var i, r, tmp = "";
for (i = 0; r = rebuild[i]; i++) {
tmp += (r == -1)? repl : ((typeof arguments[r] != "undefined")? arguments[r] : "");
}
return tmp;
});
}
alert(sx_replacer(str, "1", regex, rebuild));
Gruesse, Joachim
Am Ende wird alles gut.