评估字符串作为条件语句 [英] Evaluate string as a conditional statement

查看:83
本文介绍了评估字符串作为条件语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为初学者开发基于c ++ Web的IDE,它的核心功能之一是让用户知道以下内容的评估结果( true false )他们在我的内容可编辑div上的条件语句.

I am working on a c++ web-based IDE for beginners where one of it's core function is to let the user know the evaluation result (true or false) of the conditional statement they have on my contenteditable div.

说我已经可以获取要评估的片段"(根据我的体系结构称为片段"),并且我已经知道变量的值...

Say i am already able to fetch the "fragment" (what i call it based on my architecture) that i wanted to evaluate and i already know the values of the variables...

能否请您提出一种方法,以解决如何将片段上的文本作为条件语句,该条件语句返回true或false ...

Can you please suggest a way on how would i evaluate the text on the fragment as a conditional statement that will return either true or false...

示例

var b = 5;

$('.frag').eq(1).text() //"if( (b>1) || (b==10) )"

评估后(我需要什么帮助)应该返回true

after evaluation(what im asking for help) should return true

 $('.frag').eq(2).text() //"(b>1)" 

应返回true

 **$('.frag').eq(3).text() //"(b==10)"

应返回false

更新

我用eval看到的问题是,如果我在contenteditable div中有一个var1,而我的代码中有一个var1.我应该在contenteditable的变量上放一个标头,然后对吗?就像sc_var1一样,可以防止从我的源代码中弄乱var1?

The problem i am seeing with eval is that if i have a var1 in the contenteditable div and i have a var1 in my code. I should put a header on the variables from the contenteditable then right? like sc_var1 to prevent messing up with var1 from my source code?

推荐答案

您可以像这样使用Eval:

You can use Eval like this:

var expression = $('.frag').eq(1).text();
var result = eval(expression);

但是我通常不建议使用eval,因为总会有更好的解决方法.

However I generally do not recommend using eval because there is always a much better workaround.

如果我对您的需求有更全面的了解,我将能够提供更好的解决方案.

if I get the bigger picture of your needs I would be able to provide a better solution.

这篇关于评估字符串作为条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆