I have string like this
"(length>10)&(length<100)"
And i want this
(,length,>,10,),&,(,length,<,100,)
Is it possible get with javascript split and regex.
I have string like this
"(length>10)&(length<100)"
And i want this
(,length,>,10,),&,(,length,<,100,)
Is it possible get with javascript split and regex.
"(length>10)&(length<100)".split( /([()><&])/ ).filter( Boolean )
["(", "length", ">", "10", ")", "&", "(", "length", "<", "100", ")"]
This splits at either: (, ), >, < or & (the "or" is thanks to the [] around).
Keeping the split characters is done thanks to the capture (the parentheses around the square brackets - it's ES5 though, so not supported in IE8 and below).
Finally, to remove empty strings, I use filter( Boolean ) on the array ( ES5 too, not supported in IE8 and below either).
Instead of a split, I'd go for a global match, which behaves more like a tokenizer:
var input = "(length>10)&(length<100)";
var tokens = input.match(/\d+|[a-zA-Z]\w*|[()]|[<>=&|]+/g);
It scans the input and matches the following patterns (in order):
\d+ # one ore more digits
| # OR
[a-zA-Z]\w* # an identifier
| # OR
[()] # a single opening- or closing parenthesis
| # OR
[<>=&|]+ # one or more operators: '<=', '&', '|=', ...