It wouldn't be easy.... In fact, it would be hard, especially if you'r not used to regular expressions, parsing (sub)strings and manipulating (sub)strings, and other stuff...
To make what you want you need to make your own
eval() function so to speak,
from scratch. This means you need to take in account all the possible math operators which are to be used, the operator precedence and what not...
You need to parse each and every operator seperatly (since they can occur in any place in the expression), taking in account parenthesis and brackets and keeping everything in the correct order of precedence, etc etc.
----
To make it a bit easier, you can try to first make a parser which converts the normal math expression (called an
infix expression) into a
postfix expression (this would be the hardest part).
In that way, you can easly parse/evaluate everything from left to right instead of taking in account parenthesis, order of precedence, etc.
An example of a postfix expression is: "1 2 * 3 +", which will result in 5. Since it is converted from the normal math expression "1*2+3".
There are even already many JavaScript sources for evaluating a postfix expression (as it is actually dead easy). An example, including a very detailed explanation on how it works can be found here:
http://www.qiksearch.com/articles/cs/postfix-evaluation/index.htm
----
Infix notation
What most people understand under a mathematical expression. The operators are written in infix-style.
The major problem with this notation is that it is not that simple to parse. The parentheses surrounding the groups of operands and operators are necessary to indicate the order in which the operations are to be parsed. In the absence of parentheses, certain rules determine the order of operations.
eg1: (5+3)*9
eg2: 9*(5+3)
Postfix notation
Also called
Reverse Polish notation (RPN)
This notation is also used in some scientific calculators, mostly in HP calculators. The most noticeable difference is that the operands come
before the operator, thus leaving out the need for parentheses.
Unlike infix notation, parsing postfix notations is stack-based: operands are popped from a stack, and results are pushed back into it.
Although this might seem strange at first, postfix has the very big advantage of being extremely easy and fast.
Thus, calculations proceed from left to right.
There are no brackets or parentheses, as they are unnecessary.
Operands precede operator, they are removed as the operation is evaluated.
When an operation is done, the result becomes an operand itself (for later use for other operators).
There are no hidden states, and thus also no need to worry about checking if you have hit an operator or not.
eg1: 5 3 + 9 *
eg2: 9 5 3 + *
Convertion from infix to postfix
This is relativly hard (depends on how much experience you have on the matter). In fact, you better copy existing routines which are trialed and tested before if you don't fully understand the logic behind it.
Here is again a very easy explanation:
http://www.qiksearch.com/articles/cs/infix-postfix/
the source posted there is buggy, see the updated source here:
http://www.qiksearch.com/javascripts/infix-postfix.htm
http://www.codingforums.com/showthread.php?t=5692
Others:
http://www.cs.man.ac.uk/~pjj/cs2121/fix.html
http://www.unf.edu/~rzucker/cot3100dir/parser.html
http://trubetskoy1.narod.ru/english/alg/ppne.html