var obj = {a : 7, b : 12, c : 38}, str = "aaccddcc"; str = str.replace(/([abc])/g, function(a, b) { return b in obj ? obj[b] : b }) alert(str);