wish to escape all occurrences of "." and ":" within square brackets
[ab:1.2:ef]='12.3' => [ab\:1\.2\:ef]='12.3'
tried various permutations on replace, eg,
str.replace( /(\[.*)(\.|:)(.*\])/g, '\1\\\2\3' );
but no joy.
Both answers correct and more.
The first, a pure pattern match, does the job and is very succinct. In my opinion it answers the stated question better.
The second, calling replace with a function arg, is a wee bit more robust and offers considerably more flexibility. In my code using this approach because it handles a couple of special cases easily.