admin管理员组文章数量:1336331
I have some simple variables whose values are numeric strings:
var s = '10*10*10',
v = '60*10';
I want to turn s
into 1000
and v
to 600
.
I have some simple variables whose values are numeric strings:
var s = '10*10*10',
v = '60*10';
I want to turn s
into 1000
and v
to 600
.
- only for the multiplication operator? – patapizza Commented Jun 15, 2011 at 9:48
- 4 Where did these values e from? – Lightness Races in Orbit Commented Jun 15, 2011 at 9:48
- @tomalak it es from yaml, which is a config file. – angry kiwi Commented Jun 15, 2011 at 9:55
-
1
If you trust the contents of the config file not to contain malicious script code where you're expecting these values,
eval
is fine. – T.J. Crowder Commented Jun 15, 2011 at 9:57
5 Answers
Reset to default 7Use eval()
function:
var result = eval('10*10*10');
alert(result); // alerts 1000
If the strings are from a truly trusted source, you can use eval
to do that:
var s = '10*10*10';
var result = eval(s);
But note that eval
fires up a JavaScript parser, parses the given string, and executes it. If there's any chance that the given string may not be from a trusted source, you don't want to use it, as you're giving the source the ability to arbitrarily execute code.
If you can't trust the source, then you'll have to parse the string yourself. Your specific examples are easy, but I'm sure your actual need is more plex.
The dead easy bit:
var s, operands, result, index;
s = '10*10*10';
operands = s.split('*');
result = parseInt(operands[0], 10);
for (index = 1; index < operands.length; ++index) {
result *= parseInt(operands[index], 10);
}
...but again, I'm sure your actual requirement is more plex — other operators, spaces around the values, parentheses, etc.
Picking up on Andy E's ment below, whitelisting might well be a way to go:
function doTheMath(s) {
if (!/^[0-9.+\-*\/%() ]+$/.test(s)) {
throw "Invalid input";
}
return eval('(' + s + ')');
}
var result = doTheMath('10*10*10'); // 1000
var result2 = doTheMath('myEvilFunctionCall();'); // Throws exception
Live example
That regexp may not be perfect, I'd stare at it long and hard before I'd let any unwashed input head its way...
this could be achieved quite simply without resorting to eval
function calc(s) {
s = s.replace(/(\d+)([*/])(\d+)/g, function() {
switch(arguments[2]) {
case '*': return arguments[1] * arguments[3];
case '/': return arguments[1] / arguments[3];
}
})
s = s.replace(/(\d+)([+-])(\d+)/g, function() {
switch(arguments[2]) {
case '+': return parseInt(arguments[1]) + parseInt(arguments[3]);
case '-': return arguments[1] - arguments[3];
}
})
return parseInt(s);
}
alert(calc("10+5*4"))
You can use the eval
function to evaluate an expression in a string:
var evaluated = eval(s);
alert(evaluated)
will then alert 1000
.
If you "just" want to have these numbers out of the string you can do
eval(s)
to get "10*10*10" as a Number
本文标签: javascriptConvert string to numberStack Overflow
版权声明:本文标题:javascript - Convert string to number - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742338781a2456171.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论