admin管理员组文章数量:1025494
for example:
var s = '3+3';
s.replace(/([\d.]+)([\+\-)([^,]*)/g,
function(all, n1, operator, n2) {
r = new Number(n1) ??? new Number(n2);
return r;
}
);
note: not using eval()
for example:
var s = '3+3';
s.replace(/([\d.]+)([\+\-)([^,]*)/g,
function(all, n1, operator, n2) {
r = new Number(n1) ??? new Number(n2);
return r;
}
);
note: not using eval()
-
Looks like one of the few cases where
eval()
would be handy. – alex Commented Sep 9, 2011 at 1:03 -
Is
new Function()
off limits too? – alex Commented Sep 9, 2011 at 1:07 -
Why not using
eval()
out of curiosity? – Demian Brecht Commented Sep 9, 2011 at 1:10 - You might find the following series interesting: Essentials of Interpretation. They are small lesson about puter program interpretation, written in Javascript, the goal at the end IMO will be to implement a small scheme-like language. – Christian C. Salvadó Commented Sep 9, 2011 at 1:20
3 Answers
Reset to default 7Are Variable Operators Possible?
Not possible out of the box, but he gives a nice implementation to do it, as follows. Code by delnan.
var operators = {
'+': function(a, b) { return a + b },
'<': function(a, b) { return a < b },
// ...
};
var op = '+';
alert(operators[op](10, 20));
So for your implementation
r = operators[operator](new Number(n1), new Number(n2));
Your regex is a bit broken.
/([\d.]+)([\+\-)([^,]*)/g
should probably be
/([\d.]+)([+-])([\d+]+)/g
then you can switch on the operator:
function (_, a, op, b) {
switch (op) {
case '+': return a - -b;
case '-': return a - b;
}
}
s.replace(/(\d+)\s*([+-])\s*(\d+)/g, function(all, s1, op, s2) {
var n1 = Number(s1), n2 = Number(s2);
return (op=='+') ? (n1+n2) : (n1-n2);
});
for example:
var s = '3+3';
s.replace(/([\d.]+)([\+\-)([^,]*)/g,
function(all, n1, operator, n2) {
r = new Number(n1) ??? new Number(n2);
return r;
}
);
note: not using eval()
for example:
var s = '3+3';
s.replace(/([\d.]+)([\+\-)([^,]*)/g,
function(all, n1, operator, n2) {
r = new Number(n1) ??? new Number(n2);
return r;
}
);
note: not using eval()
-
Looks like one of the few cases where
eval()
would be handy. – alex Commented Sep 9, 2011 at 1:03 -
Is
new Function()
off limits too? – alex Commented Sep 9, 2011 at 1:07 -
Why not using
eval()
out of curiosity? – Demian Brecht Commented Sep 9, 2011 at 1:10 - You might find the following series interesting: Essentials of Interpretation. They are small lesson about puter program interpretation, written in Javascript, the goal at the end IMO will be to implement a small scheme-like language. – Christian C. Salvadó Commented Sep 9, 2011 at 1:20
3 Answers
Reset to default 7Are Variable Operators Possible?
Not possible out of the box, but he gives a nice implementation to do it, as follows. Code by delnan.
var operators = {
'+': function(a, b) { return a + b },
'<': function(a, b) { return a < b },
// ...
};
var op = '+';
alert(operators[op](10, 20));
So for your implementation
r = operators[operator](new Number(n1), new Number(n2));
Your regex is a bit broken.
/([\d.]+)([\+\-)([^,]*)/g
should probably be
/([\d.]+)([+-])([\d+]+)/g
then you can switch on the operator:
function (_, a, op, b) {
switch (op) {
case '+': return a - -b;
case '-': return a - b;
}
}
s.replace(/(\d+)\s*([+-])\s*(\d+)/g, function(all, s1, op, s2) {
var n1 = Number(s1), n2 = Number(s2);
return (op=='+') ? (n1+n2) : (n1-n2);
});
本文标签: javascriptHow to call and execute an operator from stringStack Overflow
版权声明:本文标题:javascript - How to call and execute an operator from string? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745636493a2160481.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论