var s = '3+3'; s.replace(/([d.]+)([+-)([^,]*)/g, function(all, n1, operator, n2) { r = new Number(n1) ??? new Number(n2); return r; } ); var operators = { '+': function(a, b) { return a + b }, '<': function(a, b) { return a < b }, // ... }; var op = '+'; alert(operators[op](10, 20)); r = operators[operator](new Number(n1), new Number(n2)); /([d.]+)([+-)([^,]*)/g /([d.]+)([+-])([d+]+)/g function (_, a, op, b) { switch (op) { case '+': return a - -b; case '-': return a - b; } }