Replacing only a part of regexp matching

You can use lookaround assertions:

.replace(/(?<=\.)\w+(?=\()/g, 'xxx')

Those will allow the match to succeed while at the same time not being part of the match itself. Thus you're replacing only the part in between.

The easier option for people unfamiliar with regexes is probably to just include the . and ( in the replacement as well:

.replace(/\.\w+\(/g, ".xxx(")

General answer: Capture the part that you want to keep with parentheses, and include it in the substitution string as $1.

See any regexp substitution tutorial for details.

Here: just include the . and the ( in your substitution string.

For an exercise, write a regexp that will turn any string of the scheme --ABC--DEF-- to --DEF--ABC-- for arbitrary letter-values of ABC and DEF. So --XY--IJK-- should turn into --IJK--XY--. Here you really need to use capture groups and back references.