I have a string containing numbers and operator symbols for example like this: 99*8998+-999
I need to have a space between an operator and adjacent digit like this: 99 * 8998 +- 999
I tried JavaScript replaceAll
with regex
: /(\d+)([/*+-]+)(\d*)/g
But this regex works fine until there are no non adjacent operator symbols in the string.
For example refer following working and non working conditions.
"998998+999".replaceAll(/(\d+)([/*+-]+)(\d*)/g, "$1 $2 $3")
=> "998998 + 999" // Works as expected
"998998+-999".replaceAll(/(\d+)([/*+-]+)(\d*)/g, "$1 $2 $3")
=> "998998 +- 999" // Works as expected
"99*8998+-999".replaceAll(/(\d+)([/*+-]+)(\d*)/g, "$1 $2 $3")
=> "99 * 8998+-999" // Does not works as expected. Expected: "99 * 8998 +- 999"
The regex seems to return after first match without capturing next one even with the global
flag.
What could be wrong?