vendredi 4 décembre 2020

JS Regular expression for simple mathematical expressions with correct parentheses

I need some help with a regular expression or an algorhytm in javascript, that checks if a string:

  • has the right and correct order of parentheses
  • is logical, for example: " ((9 + (76-54)) - (4/2)) +7) + (202+3) " is correct, " ((8)-2 " is not correct
  • allowed are following signs: "(",")",Numbers "[0-9]" and the basic operation signs "+","-", "*" and "/"

Rules:

  • empty parentheses arent allowed
  • no minus-values (at least that makes it a little bit easier)
  • a number can contain 0 but shouldnt start with it

I use this library for generating random expressions: https://github.com/fent/randexp.js/releases/download/v0.4.3/randexp.min.js but if someone knows a better option I would be thankful for every advice :)




Aucun commentaire:

Enregistrer un commentaire