Hello,
I am reading through one MDN page about string.replace() but then I got stuck when trying to read a snippet that is used to showcase for one example.
const p = 'The water boils when it reaches 212F.';
function f2c(x) {
function convert(str, p1, offset, s) {
return ((p1 - 32) * 5/9) + 'C';
}
let s = String(x);
let test = /(-?\d+(?:\.\d*)?)F\b/g;
return s.replace(test, convert);
}
console.log(f2c(p)) // "The water boils when it reaches 100C."
Q1: How do these parameters in convert(str, p1, offset, s) got mapped through s.replace from “test” in s.replace()? how to know what they are mapped to individually?
Q2: What does “(?:.\d*)?” do here? I am not very clear about it. Is it similar to lookahead? Isn’t “-?\d+” sufficient already?
let test = /(-?\d+(?:\.\d*)?)F\b/g;
Thank you.