I want to split basic mathematical operators from a formula string and get operands, I am using the following regex:
(/[+,-,*,/]/g)
It is working fine with +
, /
and *
but not with -
.
Sample input 1: "a+b".split(/[+,-,*,/]/g)
.
Sample output 1: ["a","b"] //Works fine
Sample input 2: "a-b".split(/[+,-,*,/]/g)
.
Sample output 2: ["a-b"] //Not working fine :( should return ["a", "b"], but returns ["a-b"]
I would love to know what I am missing in my expression.
I have an alternative i.e. iterate the whole string and returns a new array without operators but I want to use regex.
Thanks