Find and Replace - brackets to parentheses

I am trying to change brackets around numbers to parentheses.
I have used both Find & Replace and Replace Example.

I believe I have the correct regex to select the bracket, but when I replace them, I get a double parentheses on either side of the number.

[12] becomes ( )12( )

Here’s my code:

// define regex to use...
const findRegex = /\[|\]/g;
// define replacement expression...
const replaceWith = "( )";

// do the replacement...
draft.content = draft.content.replace(findRegex, replaceWith);
draft.update();

Any suggestions on what I am doing wrong, please?

Try this instead.

// define regex to use...
const findRegex = /\[(\d+)\]/g;
// define replacement expression...
const replaceWith = "($1)";

// do the replacement...
draft.content = draft.content.replace(findRegex, replaceWith);
draft.update();

You have been replacing each bracket with two parentheses.

The one I’ve suggested above looks for digits between brackets then replaces that match with the same digits but surrounded by parentheses instead of brackets.

@sylumer That worked! Thanks so much for your help!!