解析&在JavaScript中检查条件表达式
我的字符串中有可变条件,因此:
( @is_verified ='true'或@confirmation_needed!='true')和@request_limit!='0'
这只是一个示例,并且有未知数的变量&案例。
- 每个变量都以 @ @
- 有时有括号值的ors和s和shiss的
- 引号,例如'xyz',因此所有变量都可以视为字符串。
- 检查条件
JavaScript映射
const vars = {
IS_VERIFIED: 'True',
CONFIRMATION_NEEDED: 'True',
REQUEST_LIMIT: '2'
}
始终使用=或!=我也有一个 。我提出了很少的想法,但它们都太复杂了。是否有任何已知的简单方法,或者我必须从头开始创建一些东西?
感谢任何帮助,谢谢。
编辑:实现这一目标后,对我来说,下一个目标将显示哪些变量以某种方式破坏了状况。
I have string that have variable conditions in it, like this:
(@IS_VERIFIED = 'True' OR @CONFIRMATION_NEEDED != 'True') AND @REQUEST_LIMIT != '0'
This is just an example and there are unknown number of variables & cases.
- Every variable starts with @
- There are ORs and ANDs with sometimes parenthesis
- Values are always in quotation marks like 'xyz', so all can be considered strings.
- Conditions are always checked with either = or !=
I also have a javascript map which holds all the variables like:
const vars = {
IS_VERIFIED: 'True',
CONFIRMATION_NEEDED: 'True',
REQUEST_LIMIT: '2'
}
What I need to do is parse the condition string and check the values using the map above to reach a final result, true or false. I came up with few ideas but they were all too complicated. Is there any known, easy method for this or do I have to create something from scratch?
Any help is appreciated, thanks.
Edit: After achieving this, next goal for me will be showing which variables break the condition, somehow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:
eval
提前解决方案,在使用此问题时要非常小心!只需修改字符串为有效的JS表达式,然后使用
eval
。Caution:
eval
solution ahead, be very careful while using this!Simply modify the string to be a valid JS expression and then use
eval
.这是使用Regexp的片段:
我希望这使您了解如何解决问题。
Here is a snippet using RegExp:
I hope this gives you an idea about how to solve your problem.