创建一个反应本机应用程序,当前每当我输入+3或3+并按下equals按钮时,它都会产生错误,并且我想消除该错误
为此,我使用了正则表达式来匹配输入模式,但我无法弄清楚..这是我正在尝试的代码
expression: "", // variable for getting input as a string
regex = () => {
var r = this.state.expression;
var regex = /[0-9]+$/;
var regSigns = new RegExp(/[\+\-\/\*]+$/g);
if (r.match(regSigns) == r.match(regex)) {
return true;
} else {
var sim = parse(r);
sim = sim.add(0);
sim = sim.subtract(0);
sim = sim.multiply(1);
sim = sim.divide(1);
var answer1 = sim.eval({ sim }); // answer1 stores result
this.setState({ answer: answer1.toString() });
this.setState({ expression: answer1.toString() });
}
};
simplify = () => {
if (this.regex(this.state.expression)) {
}
};
每当我这样输入+3时,我得到的实际结果就是“缺少操作数” 我期望的结果是3 ...