Validate mathematical expressions using regular expression?

前端 未结 3 1435
醉酒成梦
醉酒成梦 2020-11-29 08:50

I want to validate mathematical expressions using regular expression. The mathematical expression can be this

  1. It can be blank means nothing is entered

3条回答
  •  猫巷女王i
    2020-11-29 09:22

    Something like this should work:

    ^([-+/*]\d+(\.\d+)?)*
    

    Regexr Demo

    • ^ - beginning of the string
    • [-+/*] - one of these operators
    • \d+ - one or more numbers
    • (\.\d+)? - an optional dot followed by one or more numbers
    • ()* - the whole expression repeated zero or more times

提交回复
热议问题