It should work if you remove the g flag from your regex, because then each time you call .exec() it will start afresh as explained at MDN. You can also remove the code that is messing with reMD.lastIndex.

(With the g flag, it tries to match from the index of where the previous match ended, but you are trying to match against a new string.)

Note also that you could change your regex to match the operator too, rather than using indexOf(), and you don't really need to call parseInt() because the * and / operators will coerce their operands to numbers: