I'm trying to solve a problem where I need to convert string equation to an array of numbers and operators.But I'm unable to do it.
Regex that I wrote to convert the string equation to an array.
Input: '1+2-33/45*78'.split(/([\\+\-\\*\\/]+)/)
Output: ["1", "+", "2", "-", "33", "/", "45", "*", "78"]
But the above regex doesn't work well for when you have two operators consecutive - (*-). See below for more clarification.
Input: '1+2-33/-45*-78'.split(/([\\+\-\\*\\/]+)/)
Output: ["1", "+", "2", "-", "33", "/-", "45", "*-", "78"]
I'm expecting output like below.
Input: '1+2-33/-45*-78'
Output: ["1", "+", "2", "-", "33", "/", "-45", "*", "-78"]
Edit: I looked up all the answers here on stackOverflow but they don't solve the problem I described above.
Stackoverflow answers will solve this equation like this: This string '7/-2*-9-8' will be converted to ["7", "/-", "2", "*-", "9", "-", "8"]
Please note two consecutive operators ["/-", "*-"] above.
But this is not what I'm looking for. The output I'm expecting is like:
Expected Answer: ["7", "/", "-2", "*", "-9", "-", "8]
So If have something like this '7/-2*-9-8'. Please note that I have consecutive operators("/-" or "*-") here. Then I want to include negative sign in the number itself.
All the answers in stackoverflow doesn't solve this issue. Please reopen the question.Thanks!
Why: I'm trying to implement a simple calculator with only (+, /, -, *) operators. I hope that clears some doubt.