在JavaScript中评估字符串给出布尔表达式

问题描述 投票:4回答:4

我有一个包含布尔逻辑的字符串,如:

var test = "(true)&&(false)&&!(true||true)"

在这种情况下,在JavaScript中评估此字符串以获取布尔值false的好方法是什么

  1. 我知道我们可以使用eval()或新的Function().. - 但这是一种安全的方法吗?
  2. 我猜其他选择是编写自定义解析器。作为一个相当新的人,这将是一个很大的努力吗?我找不到任何布尔逻辑表达式解析器的例子
  3. 还有其他选择吗?
javascript eval boolean-logic
4个回答
3
投票

只要你能保证它是安全的,我想你可以使用eval。

也许在做eval之前对它进行治疗?

var test = "(true)&&(false)&&!(true||true)" 

var safe = test.replace(/true/ig, "1").replace(/false/ig, "0");

var match = safe.match(/[0-9&!|()]*/ig);

if(match) {
   var result = !!eval(match[0]);
}

1
投票

Javascript有一个你可以使用的三元运算符:

var i = result ? 1 : 0;

在这里,结果是BooleanTrueFalse

所以,这个操作之后你的问题会是这样的。

(1)&(0)&!(1||1)

我希望你现在可以更好地评估这个布尔逻辑。


1
投票

你可以使用eval,例如:eval(“(true)&&(false)&&!(true || true)”);


0
投票

试试这个代码

function processExpression(expr)
{
  while (expr.indexOf("(" ) != -1 )
  {
    expr = expr.replace(/\([\w|]+\)/g, function(matched){ return processBrace(matched)});
  }
  return expr = processBrace( "(" + expr + ")" );
}
function processBrace(str)
{
    return str.substring(1).slice(0,-1).split(/(?=&|\|)/).map(function(value,index,arr){ 
        if ( index != 0 && index%2 == 0 ) { return arr[index-1] + value } else if(index==0){return value;} else {return ""}
    }).filter(function(val){return val.length > 0}).reduce(function(prev,current){
        var first = Boolean(prev);
        var operator = current.substring(0,2);
        var operand = current.substring(2); 
        while ( operand.indexOf("!") != -1 )
        {
           var boolval = operand.match(/\w+/)[0] == "false"; //flip the value by comparing it with false
           var negations = operand.match(/\W+/)[0];
           operand = negations.substring(1) + boolval;
        }
        var second = operand == "true";
        var output = operator == "&&" ? (first && second) : (first || second); 
        return output;
    });
}

DEMO

    function processExpression(expr)
    {
      while (expr.indexOf("(" ) != -1 )
      {
    	expr = expr.replace(/\([\w|]+\)/g, function(matched){ return processBrace(matched)});
      }
      return expr = processBrace( "(" + expr + ")" );
    }
    function processBrace(str)
    {
    	return str.substring(1).slice(0,-1).split(/(?=&|\|)/).map(function(value,index,arr){ 
    		if ( index != 0 && index%2 == 0 ) { return arr[index-1] + value } else if(index==0){return value;} else {return ""}
    	}).filter(function(val){return val.length > 0}).reduce(function(prev,current){
    		var first = Boolean(prev);
    		var operator = current.substring(0,2);
    		var operand = current.substring(2); 
    		while ( operand.indexOf("!") != -1 )
    		{
    		   var boolval = operand.match(/\w+/)[0] == "false"; //flip the value by comparing it with false
    		   var negations = operand.match(/\W+/)[0];
    		   operand = negations.substring(1) + boolval;
    		}
    		var second = operand == "true";
    		var output = operator == "&&" ? (first && second) : (first || second); 
    		return output;
    	});
    }


var example1 = "(true)&&(false)&&!(true||true)";
document.body.innerHTML += example1 + " -- " + processExpression(example1);
© www.soinside.com 2019 - 2024. All rights reserved.