A split
and/but non regex based approach can be implemented as straightforward as the following code (of cause having no control over the validity of the parsed 3rd argument which is assumed to be a digit (sequence)) ...
const sampleData = [
'1.01.01.02',
'1.01.02.03',
'1.01.0x.04',
'1.01.0x.0x',
'1.01.001.01.07.08.09',
'1.01.013.03.01200230',
];
console.log(
sampleData, '=>',
sampleData.map(item => {
const [leadingA, leadingB, digits, ...trailing] = item.split('.0');
return [
leadingA, leadingB, (parseInt(digits, 10) + 1), ...trailing
].join('.0');
})
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
A more precise (edge cases) replace
based approach is in need of a more complex capturing
regex like ... /^((?:.*?\.0){2})(\d+)(.*)/
... and a replacer function as its second argument.
The regex ... /^((?:.*?\.0){2})(\d+)(.*)/
... reads like this ...
Part 1: ^( (?:.*?\.0){2} )
- From line start
^
capture ( ... )
the following match ...
(?: ... ){2}
... twice {2}
a not captured ?:
group ( ... )
of following pattern ...
.*?0
... anything .*
in a non ?
greedy way until a Dot-Zero sequence .0
occurs.
Part 2: (\d+)
- Capture
( ... )
at least one +
digit \d
.
Part 3: (.*)
- Capture
( ... )
anything .*
greedy until the end of the string.
The related replacer function is the callback of replace
and gets passed as arguments the entire match (1st argument) followed by each captured value as separate argument, starting with the first and ending with the last capture.
Thus for the given example the replacer function's argument names (which can be freely chosen) and precedence are ... (match, leading, digits, trailing)
... where digits
is the 2nd capture group, the digit (sequence) which the OP wants to become an incremented integer.
A solution then might look similar to the next provided example code ...
const sampleData = [
'1.01.01.02',
'1.01.02.03',
'1.01.0x.04',
'1.01.0x.0x',
'1.01.001.01.07.08.09',
'1.01.013.03.01200230',
];
// see ... [https://regex101.com/r/60omn1/2]
const regXCapture = (/^((?:.*?\.0){2})(\d+)(.*)/);
console.log(
sampleData, '=>',
sampleData.map(item =>
item.replace(
regXCapture,
(match, leading, digits, trailing) => [
leading,
parseInt(digits, 10) + 1,
trailing,
].join('')
)
)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }