如何同时进行 Typescript 正则表达式替换和提取值?

How to do Typescript regex replace and extract value in the same time?

我想将路径 = 'a.0.b.c.e.f.11.d.20' 更改为 'a[0].b.c.e.f[11].d[20]'

我想使用类似的东西:replace(/\.\d+/g , '.[${d+}]');

但这不起作用。如何将 .number 替换为值 .[number]?

匹配句点后,捕获组中的数字,然后使用捕获组替换括号中的那些数字:

console.log(
  'a.0.b.c.e.f.11.d.20'
    .replace(/.(\d+)/g, '[]')
);