Have you tried, instead:

``` js
str.replace(/\$(\d)/i, function(i) {
  return m[i]
});
```