Hello there, I am attempting to break down a string of text into an array whenever a '+' or '-' is present. First of all, I am looking for a way to split at the plus sign and ensure it is included in the array.
I have experimented with this approach: '3+5'.split(/(?='+')/)
and I am expecting the output to be ["3","+5"]
However, it doesn't seem to be working as desired and only gives me an array of ["3+5"]
which is fine but not what I require. All the sources I have consulted suggest that this method should work, but it's not providing the outcome I need. I tried replacing the plus sign with commas and it worked well, but I specifically need it to work with the plus sign.
Secondly, I would like to also split the string at a minus sign in a similar manner. Since the plus sign method didn't yield the desired result, I don't have code for it, but based on what I've found, I assume it would look something like this: '3+5-2'.split(/(?=+) | (?=-)/)
with the expected array of ["3","+5","-2"]
.