0

I'd like to change path = 'a.0.b.c.e.f.11.d.20' to 'a[0].b.c.e.f[11].d[20]'

I was thinking to use something like : replace(/\.\d+/g , '.[${d+}]');

But this is not working. How can I replace the .number with with value .[number]?

0

1 Answer 1

2

After matching a period, capture digits in a group, then replace with those digits in brackets using the capture group:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.