so explaining the example:
'fabio'.replace(/(fa)/g, '$1a'); // returns "faabio"
Ive created one group (fa) and then on the replace string i reference it by using a dollar sign followed by its index, which is 1 (0 is for the complete match, which is 'fa' too hehe).
You can reference a group into the current regex too, like this:
'fafafabio'.replace(/(fa)\1\1/g, '$1a'); // returns "faabio" too
Just replace the dollar sign by a backslash, the index works the same way.
--
Fábio Miranda Costa
Solucione Sistemas
Engenheiro de interfaces