I'm trying to implement some kind of dynamic filtering.
Let's say I have a collection of objects. Each objects have same keys but different values.
Ex :
{
"state":"time out",
"displayState": "error"
}
I want to filter and categorize them following a pattern extracted from a string.
Ex (no meaning at all, just extrapolated) :
"displayState=error&(state!=aborted|(state=cancelled&state=timed out))"
I think the best way to instrument this string would be via regular expression. To be able to catch groups, operands and operators
([^|&!()=<>]*)([=!<>]{1,2})([^|&!()=<>]*)(?:([|&])\(?([^|!&()=<>]*)([=!<>]{1,2})([^|&!()=<>]*)\)?)?
It's basic and linear, my knowledge in regexp is a limited, so it's not doing what I need.
Basically I'm trying to group by ()
first and then by [.*][=><!][.*]
.
Catching groups, operands and operators in the same process.
- EDIT -
Thanks to Aniket's answer I was able to get a little further.
As stated in these answers, regular expressions can't do recursion, at least, not in Javascript.
So groups delimited by ()
cannot be isolated only by regexp, and needs some logic.
I've reviewed Aniket's regexp to clean catches
/([&|])?\(*(([a-zA-Z0-9 ]*)([!=<>]+)([a-zA-Z0-9 ]*))\)*/g
which will return
0 : {
expression : displayState=error
type : undefined
operand1 : displayState
operator : =
operand2 : error
},
1 : {
expression : &(state!=aborted
type : &
operand1 : state
operator : !=
operand2 : aborted
},
2 : {
expression : |(state=cancelled
type : |
operand1 : state
operator : =
operand2 : cancelled
},
3 : {
expression : |state=timed out))
type : |
operand1 : state
operator : =
operand2 : timed out
}
I'm working on isolating groups with javascript, and have a complete jsFiddle workflow.
I'll post my solution once it's working properly.