You can do:
str.replace(/\((\d+)\)/g, "[$1]");
Used expression: \((\d+)\)
(and )are special char in regex used to group to match literals
( ), you need to avoid them as
\( \)\dis short for numbers. \d+
means one or more digits.( )- group and remember the number. The memorized number will be used later in return.gfor global replacement. I mean every appearance of the patten in the string will be replaced.$1 - (
), ./ / .