Parsing AND and OR condition statement based on a parentheses in javascript/angularjs?

1k views Asked by At

Ok so basically, I have a statement as such;

$scope.promotion = "((A|B)|(C|D)) & (E | ((F|G) & (H|I))) & (J | K)";

is it possible to separate this string based on the parentheses's? so that i get a return as such;

$scope.promo1 = "(A|B)";
$scope.promo2 = "(C|D)";
$scope.promo3 = "((A|B) | (C|D))";

something like this.

1

There are 1 answers

0
Udhayan Nair On

so after researching a bit, i would say this is one solution on how to break up the string based on a mathematical logic.

$scope.array1 = [];
$scope.array2 = [];
var txt1 = "((A|B)|(C|D)) & (E | ((F|G) & (H|I))) & (J | K)";

for(var i=0; i < txt1.length; i++){
   if(txt1.charAt(i) === '('){
     $scope.array1.push(i);
   }
   if(txt1.charAt(i) === ')'){
     $scope.array2.push(txt1.substring($scope.array1.pop()+1,i));
   }
} 

Hence, it array2 would return something like; ["A|B", "C|D"] and so on.