How do I stick a counter in a javascript regular expression substitution?

The question is [answered here](https://stackoverflow.com/a/4214173/823263) for Perl/PCREs.

I've tried the obvious `string.replace(/from/g, "to "+(++count))`, which is no good (the ++count is evaluated once at the start of the string.replace, it seems).