I have been so puzzled and stuck in this small code below.
I use replacer method provided by JavaScript replace() but it puzzles me why the 2nd operand becomes 2 instead of 2.1.
Any idea if I missed anything or made any mistake below?
Thank you.
let equation = "1.2+2.1"
// Regex pattern for example: 2+2 or 2.2+2.1
const addRegex = /(\d+|\d+\.\d+)(\+)(\d+|\d+\.\d+)/
const addReplacer = (match, p1, p2, p3, offset, string) => {
console.log(parseFloat(p1)) // 1.2
console.log(parseFloat(p3)) // 2 <--- This is the puzzling problem. This should be 2.1 instead of 2
console.log(parseFloat(p1) + parseFloat(p3)) // 3.2 <--- incorrect due to incorrect 2nd operand above
return parseFloat(p1) + parseFloat(p3)
}
while (addRegex.test(equation)) {
console.log(equation) // "1.2+2.1"
equation = equation.replace(addRegex, addReplacer)
console.log(equation) // 3.2.1 <-- Also don't know why this changes from 3.2 to 3.2.1 here
}
I see. By the same logic, the problem could be resolved by adding a “$” in the end of Regex:
/(\d+|\d+\.\d+)(\+)(\d+|\d+\.\d+)$/
But then this will force the search to skip if the addition operation is not in the end of equation and always only find the addition in the end of equation, which could be problematic.
Any better workaround to have this handle both integers and decimals in the same Regex pattern except setting four different Regex search patterns for four operation cases (integer + integer, integer + decimal, decimal + integer, decimal + decimal)?