I have the following string:
[group][100][250][3][person]
and I need to increment the number 3. I tried the regex /\[\d+\]/ which matches all 3, but I couldn't get anywhere from here.
You could do it by matching all 3 of your numeric values and just increment the third:
var regex = /\[(\d+)\]\[(\d+)\]\[(\d+)\]/g
var input = "[group][100][250][3][person]";
var result = input.replace(regex, function(match,p1,p2,p3){
return "[" + p1 + "][" + p2 + "][" + (parseInt(p3,10) + 1) + "]"
})
console.log(result);
To capture "your" string, try the below regex:
(?:\[\d+\]){2}\[(\d+)\]
How it works:
(?:...){2} - a non-capturing group, occuring 2 times.\[\d+\] - containing [, a sequence of digits and ].[, a capturing group - sequence of digits and ].The text you want to capture is in the first capturing proup.
100and250could be123456and654321for example. They could be any length.[3]and not[100]or[250]. Reading your previous question I suspect that you choose this one for its position, however you don't say if you want the last or the third.