Skip to content
Advertisement

How to replace square brackets and set dot before replacement?

Good day.

I wish to replace square brackets inside string and set dot before replacement.

For example, Current string position[0].type i need transform to next one position.0.type

I have tried making it by this rule 'position[0].type'.replace(/[[]]/g, '') as expected, will removed only brackets, how i can set needed dot?

Thanks.

Advertisement

Answer

You can use

.replace(/[[].]+/g, '.')

The [[].]+ regex matches one or more (+) [, ] or . chars. g makes it match all occurrences and '.' replaces the matches with a single ..

Advertisement