如何在 javascript 中用正则表达式解析一个简单的算术运算?
How to parse a simple arithmetic operation with regex in javascript?
我正在使用AngularJS制作主页,并且有算术功能。
<div class="input-form">
<input ng-model="value1" id="value1" type="number" />
<input ng-model="value2" id="value2" type="number" />
<input ng-model="value3" id="value3" type="number" />
<input ng-model="value4" id="value4" type="number" />
<input ng-model="value5" id="value5" type="number" />
</div>
<div class="equation-form">
<input ng-model="equation" type="text" />
</div>
<button class="yellow" ng-click="calculate()">Calculation</button>
如果用户在方程字段输入算术方程后按“计算”按钮,需要计算结果并发送服务器。
公式输入如"1 + 2 - 3 * 4 + 5"
。
1,2,3,4,5 values 表示输入值命名为 value1, value2, value3, value4, value5
这是我试图实现的计算:
scope.calculate = function () {
let equation = scope.equation.replace(/\s+/g, ''); // remove spaces
if (!/^\d+(?:[+-]\d+)*$/.test(expression)) { //
console.error('wrong equation');
return;
}
let fieldIndexes = expression.split(/(?=[+-])/); // split expression
if (fieldIndexes) {
fieldIndexes.forEach(x => {
// process calculation
});
}
}
函数分两步完成:
首先,将等式拆分为 [ 1, +2, -3, *4, 5 ]。
其次,计算拆分方程。
但是现在,我只用“-”、“+”进行了拆分。
如果用户输入 "1 + 2 - 3 + 4 - 5"
,当前函数将其拆分为 "1", "+2", "-3", "+4", "-5"
.
如何用“-”、“+”、“*”、“/”符号分割字符串?
有什么建议吗?
好吧,我写了一些不使用 RegExp 的东西。
class Operation {
constructor(data) {
this.data = data;
}
result(items = null) {
for (var i = 0; i < items.length; i++) {
items[i] = items[i].split(this.data.op);
if (this.data.next) {
items[i] = this.data.next.result(items[i]);
}
items[i] = items[i].reduce(this.data.reducer);
}
return items;
}
}
function calculate(expr) {
var mult = new Operation({ op: "*", next: null, reducer: (f, l) => { return parseInt(f) * parseInt(l) } });
var div = new Operation({ op: "/", next: mult, reducer: (f, l) => { return parseInt(f) / parseInt(l) } });
var sub = new Operation({ op: "-", next: div, reducer: (f, l) => { return parseInt(f) - parseInt(l) } });
var add = new Operation({ op: "+", next: sub, reducer: (f, l) => { return parseInt(f) + parseInt(l) } });
return add.result([expr.replace(/\s+/g, "")])[0];
}
您为计算函数提供算术表达式,它 returns 为您计算结果。
每个操作层将操作数 - “项目” - 传递给下一个更高阶的操作,直到它到达最后一个(这里是乘法)。
示例:
console.log(计算("6 * 5 - 2"));
现在添加括号应该很简单了。
有帮助吗?
我正在使用AngularJS制作主页,并且有算术功能。
<div class="input-form">
<input ng-model="value1" id="value1" type="number" />
<input ng-model="value2" id="value2" type="number" />
<input ng-model="value3" id="value3" type="number" />
<input ng-model="value4" id="value4" type="number" />
<input ng-model="value5" id="value5" type="number" />
</div>
<div class="equation-form">
<input ng-model="equation" type="text" />
</div>
<button class="yellow" ng-click="calculate()">Calculation</button>
如果用户在方程字段输入算术方程后按“计算”按钮,需要计算结果并发送服务器。
公式输入如"1 + 2 - 3 * 4 + 5"
。
1,2,3,4,5 values 表示输入值命名为 value1, value2, value3, value4, value5
这是我试图实现的计算:
scope.calculate = function () {
let equation = scope.equation.replace(/\s+/g, ''); // remove spaces
if (!/^\d+(?:[+-]\d+)*$/.test(expression)) { //
console.error('wrong equation');
return;
}
let fieldIndexes = expression.split(/(?=[+-])/); // split expression
if (fieldIndexes) {
fieldIndexes.forEach(x => {
// process calculation
});
}
}
函数分两步完成:
首先,将等式拆分为 [ 1, +2, -3, *4, 5 ]。
其次,计算拆分方程。
但是现在,我只用“-”、“+”进行了拆分。
如果用户输入 "1 + 2 - 3 + 4 - 5"
,当前函数将其拆分为 "1", "+2", "-3", "+4", "-5"
.
如何用“-”、“+”、“*”、“/”符号分割字符串?
有什么建议吗?
好吧,我写了一些不使用 RegExp 的东西。
class Operation {
constructor(data) {
this.data = data;
}
result(items = null) {
for (var i = 0; i < items.length; i++) {
items[i] = items[i].split(this.data.op);
if (this.data.next) {
items[i] = this.data.next.result(items[i]);
}
items[i] = items[i].reduce(this.data.reducer);
}
return items;
}
}
function calculate(expr) {
var mult = new Operation({ op: "*", next: null, reducer: (f, l) => { return parseInt(f) * parseInt(l) } });
var div = new Operation({ op: "/", next: mult, reducer: (f, l) => { return parseInt(f) / parseInt(l) } });
var sub = new Operation({ op: "-", next: div, reducer: (f, l) => { return parseInt(f) - parseInt(l) } });
var add = new Operation({ op: "+", next: sub, reducer: (f, l) => { return parseInt(f) + parseInt(l) } });
return add.result([expr.replace(/\s+/g, "")])[0];
}
您为计算函数提供算术表达式,它 returns 为您计算结果。 每个操作层将操作数 - “项目” - 传递给下一个更高阶的操作,直到它到达最后一个(这里是乘法)。
示例: console.log(计算("6 * 5 - 2"));
现在添加括号应该很简单了。
有帮助吗?