The RegEx: "\[\d+\]"
\[ First, an open square bracket (escaped)
\d This means a digit (this is the same as [0-9])
+ The plus means there can be as many digits as you wish
\] Ending it up with an escaped close bracket.
Note: We can use /\[\d+\]/g as a replacement for new RegEx(regex) and another benefit is that we don't have to double escape the characters (escaping the character in RegEx, and also escaping the backslash so JS will treat it as a normal backslash).
Change replaceThe to replace the nth occurrence.
var string = 'sometext[2][markers][2][title]';
var replaceThe = 2; // Replace the -second- occurrence
var i = 0;
string = string.replace(/\[\d+\]/g,
function(m) { return (replaceThe == (++i) ? '[3]' : m); })
The last line uses an overload of the string.replace function which accepts a function.
This function will be called for each match and the m parameter will be set to the match itself. The function is expected to return a replacement for the match. This way we can wait until the nth occurrence until we actually replace the match, otherwise we return the match we received, and that way it won't be replaced.