87,955
社区成员
![](https://csdnimg.cn/release/cmsfe/public/img/topic.427195d5.png)
![](https://csdnimg.cn/release/cmsfe/public/img/me.40a70ab0.png)
![](https://csdnimg.cn/release/cmsfe/public/img/task.87b52881.png)
![](https://csdnimg.cn/release/cmsfe/public/img/share-circle.3e0b7822.png)
function test() {
x = "3+2";
if (x.indexOf("+") > 0) {
left = parseInt(x.match(/^(.*)\+/)[1]);
right = parseInt(x.match(/\+(.*)$/)[1]);
x = left + right;
} else if (x.indexOf("-") > 0) {
left = parseInt(x.match(/^(.*)\-/)[1]);
right = parseInt(x.match(/\-(.*)$/)[1]);
x = left - right;
} else if (x.indexOf("*") > 0) {
left = parseInt(x.match(/^(.*)\*/)[1]);
right = parseInt(x.match(/\*(.*)$/)[1]);
x = left * right;
} else if (x.indexOf("/") > 0) {
left = parseInt(x.match(/^(.*)\//)[1]);
right = parseInt(x.match(/\/(.*)$/)[1]);
x = left / right;
}
return x;
}
function test() {
var x = "3+2";
var left = parseInt( x.match(/^\d+/)[0] );
var right = parseInt( x.match(/\d+$/)[0] );
var op = x.match(/[^\d](?=\d+$)/)[0];
switch(op){
case '+':
return left + right;
case '-':
return left + right;
...
}
}
function test() {
var x = "3+2";
return eval(x);
}