从阵列中构建布尔表达,并在JavaScript中一次评估整个表达
我有两个数组 - 一个包含布尔值,另一个运算符:
to_eval = [true, true, false, false]
ops=['&&', '||', '&&']
我想构建一个表达式,
result = true && true || false && false
如果我迭代阵列在我走时评估的阵列,则应根据优先规则进行评估
,结果是false 。
我可以构建一个字符串并使用评估或功能 - 但是根据我一直在阅读的内容,可能会在Web应用程序中引起安全问题。
在JavaScript中,是否有一种方法可以在不评估它的情况下构建表达式,直到完全形成为止,以便在不使用ERAT或功能的情况下观察优先规则?
I have two arrays - one that contains booleans, and the other operators:
to_eval = [true, true, false, false]
ops=['&&', '||', '&&']
Out of this I'd like to build an expression
result = true && true || false && false
Which should evaluate to true based on precedence rules
If I iterate over the arrays evaluating as I go, the result is false.
I could build a string and use Eval or Function - but from what I have been reading this can cause security issues in a web application.
Is there a way in Javascript to build out an expression without evaluating it till it is fully formed so that precedence rules are observed without using Eval or Function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果确定您的两个阵列具有预期值(布尔值和预期运算符),则可以安全地调用
est
。因此,只需添加一些代码即可验证两个给定的输入。您可以按照以下操作:
You can safely call
eval
, if it is certain that your two arrays have the expected values (booleans and expected operators). So just add some code to verify the two given inputs.You can do as follows:
您可以使用
数组#redard
:有了适当的秘密化,您可以安全地使用
eval
:You can create a mapping functions for the trusted logic operators safely with
Array#reduce
:With proper santization, you can safely use
eval
:如果您对输入进行消毒(即,验证所有令牌是否来自非常具体的令牌列表),则可以使用评估。
这远非您可以编写的最有效的代码,但是它很简单并且完成工作,并且对其他操作员和括号添加支持是微不足道的。
另外,您可以根据自己想要的优先顺序自己评估操作员:
如果您要增加许多操作员的支持,您可能希望使此代码更加健壮,重复性降低,但这至少应该使您启动。
You could use eval if you sanitize the input (IE, verify that all the tokens are from a very specific list of allowed tokens).
This is far from the most efficient code you could write, but it's simple and gets the job done, and adding support for other operators and parentheses is trivial.
Alternatively, you could evaluate the operators yourself, in the order of precedence you desire:
You probably would want to make this code more robust and less repetitive if you were to add support for many operators, but that should get you started at least.