JavaScript函数添加一串数字

时间:2021-12-24 01:58:56

Not sure how to word this and so am not having much luck in Google searches...

不知道怎么说这个,所以在Google搜索中没有太多运气...

I need to calculate a value for a string of numbers.

我需要为一串数字计算一个值。

Example. A user types in "1.00 + 2.00 - 0.50" into a text field. The function should be able to take that string and calculate the result to return $2.50.

例。用户在文本字段中键入“1.00 + 2.00 - 0.50”。该函数应该能够获取该字符串并计算结果以返回$ 2.50。

Anyone have a way to do this in their bag of tricks?

任何人都有办法在他们的技巧包里做到这一点?

3 个解决方案

#1


If it's actually a mathematical operation you may just use eval, not sure that's what you want though :

如果它实际上是一个数学运算,你可能只是使用eval,但不确定这是你想要的:

document.write(eval("1.00 + 2.00 - 0.50"));

#2


Theo T's answer looks like the most straightforward way, but if you need to write your own simple parser, you can start by splitting the string and looking for your operators in the output.

Theo T的答案看起来是最简单的方法,但是如果你需要编写自己的简单解析器,你可以先分开字符串并在输出中查找你的运算符。

#3


Just to improve Theo's answer.

只是为了改善Theo的答案。

You should NOT be using eval unless you're absolutely sure what you are passing to that function. Since eval will run the code, you can write any JavaScript code and it will be executed.

你不应该使用eval,除非你完全确定你传递给该函数的是什么。由于eval将运行代码,因此您可以编写任何JavaScript代码并执行它。

One of the ways of making sure you will only get the right code is using the following script:

确保您只获得正确代码的方法之一是使用以下脚本:

var str = "$34.00 + $25.00 alert('some code')"; // Our string with code
var pattern = /[0-9\+\-\.\*\/]*/g; // Regex pattern
var math_operation = str.match(pattern).join(''); // Getting the operation
document.write(eval(math_operation));​ // Getting the result

This not only allows the user to type things like $34.00 + $5.00 but also prevents from code being injected.

这不仅允许用户键入$ 34.00 + $ 5.00之类的内容,还可以防止代码被注入。

#1


If it's actually a mathematical operation you may just use eval, not sure that's what you want though :

如果它实际上是一个数学运算,你可能只是使用eval,但不确定这是你想要的:

document.write(eval("1.00 + 2.00 - 0.50"));

#2


Theo T's answer looks like the most straightforward way, but if you need to write your own simple parser, you can start by splitting the string and looking for your operators in the output.

Theo T的答案看起来是最简单的方法,但是如果你需要编写自己的简单解析器,你可以先分开字符串并在输出中查找你的运算符。

#3


Just to improve Theo's answer.

只是为了改善Theo的答案。

You should NOT be using eval unless you're absolutely sure what you are passing to that function. Since eval will run the code, you can write any JavaScript code and it will be executed.

你不应该使用eval,除非你完全确定你传递给该函数的是什么。由于eval将运行代码,因此您可以编写任何JavaScript代码并执行它。

One of the ways of making sure you will only get the right code is using the following script:

确保您只获得正确代码的方法之一是使用以下脚本:

var str = "$34.00 + $25.00 alert('some code')"; // Our string with code
var pattern = /[0-9\+\-\.\*\/]*/g; // Regex pattern
var math_operation = str.match(pattern).join(''); // Getting the operation
document.write(eval(math_operation));​ // Getting the result

This not only allows the user to type things like $34.00 + $5.00 but also prevents from code being injected.

这不仅允许用户键入$ 34.00 + $ 5.00之类的内容,还可以防止代码被注入。