Learn Form Validation by Building a Calorie Counter - Step 31

Hello,
Here is the URL of the step:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/learn-form-validation-by-building-a-calorie-counter/step-31

It says

Number inputs only allow the e to occur between two digits. To match any number, you can use the character class [0-9]. This will match any digit between 0 and 9.

Add this character class before and after e in your pattern.

After playing around with regex I wasn’t able to find the logic of adding [0-9] in the pattern. For example

console.log(“1.23E+10”.replace(/e/i, “”))

would result in 1.23+10 and

console.log(“1.23E+10”.replace(/e+/i, “”))

would result in 1.2310
Using a real invalid number:

console. log(“1.23E+10e455e”.replace(/[e+]/gi, “”))

would result in 1.2310455

but the expected output isn’t returned when using [0-9].
for example

console. log(“1.23E+10e455e”.replace(/[0-9]e[0-9]/gi, “”))

results in (for me) an unexpected output: 1.23E+155e

same for

console. log(“1.23E+10e455e”.replace(/[0-9]+e[0-9]+/gi, “”))

which results in 1.23E+e

I wonder what I’m missing. Thanks in advance!

not really, unless you escape the +

I guess the regex may be made better? But the numbers used in the project do not use + in front of the exponent, so that could be where your confusion is.

this patter could be made better with an optional ± in front of the exponent, you are right.

Question here, why are you using replace to test the regex? wouldn’t match be better?

are you asking what is the purpose of checking for a digit before and after the e?
Because this project wants to use this function to check for that.
(it isn’t to do any kind of replace. It is just to confirm that the letter e is sandwiched between two digits)

not really, unless you escape the +

I guess the regex may be made better? But the numbers used in the project do not use + in front of the exponent, so that could be where your confusion is.

Many thanks for your reply! Looks like I missed there the escape, wasn’t intentional.

Question here, why are you using replace to test the regex? wouldn’t match be better?

Would sound strange, but I’m not that far in the curriculum. At this point I don’t know yet the functionality of “match”.^^

Thank you for your answer!

are you asking what is the purpose of checking for a digit before and after the e?

Yes and no. I just had the idea using it in replace function and play around to see what it does.

(it isn’t to do any kind of replace. It is just to confirm that the letter e is sandwiched between two digits)

Yes. But (maybe a silly question), is it technically possible to build a logical example using [0-9] in combination with the replace function which clearly demonstrates the use of it?

Which clearly demonstrates the use of replace? Why not just look it up?

I was referring to the [0-9] pattern initially - but this is now sorted anyway. I kept playing around and researching, and was able to erase the question marks in my mind. Many many thanks again for your support! :raised_hands:

1 Like