使用正则表达式拆分算术表达式

时间:2017-05-05 09:40:24

标签: javascript regex arithmetic-expressions

我想将protected override void OnSourceInitialized(EventArgs e) { // For added compatibility, turn off hardware rendering HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource; HwndTarget hwndTarget = hwndSource.CompositionTarget; hwndTarget.RenderMode = RenderMode.SoftwareOnly; base.OnSourceInitialized(e); } 这样的表达式拆分为using System.Windows.Interop;

-1*-0.8/5-5这就是我现在使用[ '-1', '*', '-0.8', '/', '5', '-', '5' ]

获取的内容

对此有何建议?

1 个答案:

答案 0 :(得分:3)

这是一个解决方案。它正确检测到-/*([*\/]|\b\s*-|\b\s*\+) 并接受使用空格:

var expression = "-1*-0.8/5-5";
console.log(expression.split(/([*\/]|\b\s*-|\b\s*\+)/g));



var expression = "-1 * -0.8 / (5 - 5)";
console.log(expression.split(/([*\/()]|\b\s*[-+])/g));




filter that handles the authorization

Demo on regex101 ,这是一个改进的接受括号



JOIN




Wiktor's comment