Check If Input String Is a Valid Math Expression

I’m going to check if input string is a valid mathematical expression. I have used eval, Number and parseFloat, but none of them worked correctly as shown bellow:

  • I expect eval('1//2') to identify that the input is NOT valid, but it results in 1 which is not correct for me.
  • I also expect Number('2.5/6') to be 0.4166666666666667, but it results in NaN which is not correct for me.
  • I also expect parseFloat('1//2') to identify that the input is NOT valid, but it results in 1 which is not correct for me.

Leave a Comment