我有一个函数,它把一个表达式或方程分解成个别的术语。
这就是我所拥有的:
const terms = equation => {
equation = equation.replace(/\s/g, '').replace(/(\+\-)/g, '-').split(/(?=[\+\-\=])/g);
for (var i = 0; i < equation.length; i++) {
if (equation[i].indexOf('=') != -1) {
equation[i] = equation[i].replace('=', '');
equation.splice(i, 0, '=');
}
}
return equation;
}
console.log(terms('2(a+6-2)+3-7 = 5a'));
这将记录"2(a", "+6", "-2)", "+3", "-7", "=", "5a"
我要它记录"2(a+6-2)", "+3", "-7", "=", "5a"
如果它在圆括号中,我如何使它在'+'或'-'上不分裂?
发布于 2020-12-06 00:29:53
感谢谁的评论
最后我用了这个:
const terms = equation => equation.replace(/\s/g, '').replace(/(\+\-)/g, '-').match(/\w*\([^)(]+\)|=|[+-]?\s*\w+/g);
console.log(terms("2(a+6-2)+3-7 = 5a"));
https://stackoverflow.com/questions/65162752
复制相似问题