Javascript: check if an expression is evaluable or not?
I have a random expression that is given by users (for example: 2 + 3 * 5.21 * + (4) 7, * - 54 + 3). This expression can contain any number of operands or operators to form an expression. I need to evaluate this expression to get an answer. I tried to evaluate using the eval () function , but the problem is that the wrong expression passed to the eval () function , it throws an error and stops the program. I tried it with
if(eval(exp))
{
//Action Expression is evaluatable
}
else
{
//expression is not Evaluatable
}
but didnt work and issued an error
"SyntaxError: unterminated regular expression literal."
Due to the variety in the nature of the expressions, it would be difficult for me to construct control statements before evaluating.
can you suggest how I can just check if the expression is passed to the eval () function correctly?
source to share
You can put the content of your script in a script tag on the fly and then check the return of the error function. Or define a variable at the end of your script and check if it exists after execution.
Or mayber you can find the answer here: fooobar.com/questions/121603 / ... !!
source to share