Overview: JS's eval (str) can be used to return the evaluated results of a mathematical expression, but the brackets "[" and "]" of the expression denote the connector, unlike the "curly braces, after brackets," in the arithmetic, the braces "{" cannot be used,
So the brackets and braces of STR must be replaced with the corresponding parentheses.
Java Code Execution JS:
Public Static voidMain (string[] args) {Scriptenginemanager factory=NewScriptenginemanager (); ScriptEngine Engine= Factory.getenginebymimetype ("Text/javascript"); String str1= "12*3+[20/5*3-(7+math.exp (1))"; String str2= Str1.replace ("[", "("). Replace ("]", ")"); Try{System.out.println (Engine.eval (str1)); System.out.println (Engine.eval (str2)); } Catch(scriptexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }
Operation Result:
362.281718171540955438.28171817154096
The eval () function of JS and its Java implementation (computational mathematical expressions)