87,965
社区成员
发帖
与我相关
我的任务
分享
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);
}