Click here to Skip to main content
14,932,270 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I need a help in writing a validator in javascript or JQuery for a string expression that will allow arithmetic expressions like (),+,-,*,/,min(),max(),avg().
In the string, min(),max(),avg() should allow only two arguments.
Sample string expression to validate :
(x+y+max(min(x,y),z)+avg(x,y)*z/y)

What I have tried:

First I have tried a syntax parser and trying to tokenize the strings and validate individually but didn't work. Is there any way to do it?
Posted
Updated 20-Jul-18 0:42am
Comments
Jochen Arndt 20-Jul-18 6:18am
   
Your first approach using a (recursive) tokeniser and checking the syntax when a token can't be broken down anymore is the way to do it.

1 solution

No, there is no "simple" method to do it: as Jochen says, you need to recursively apply a tokeniser and then analyse the syntax.

There are many examples available doing just that - normally as the first stage of an expression evaluator. Google will find you plenty: expression evaluator javascript - Google Search[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900