I was developing a function that took a logical sequence of characters, let's call it a query, this query represents simple expressions separated by ;
, expressions can contain any of these operations: ==
, !+
, >
, <
, <=
, =>
, ~=
representing a validation between attribute and value, I need to get this string and separate it into an array of objects with each of these separate properties. / p>
For example:
"temperature == 40; engine! = fail; speed> 90; speed = 90; speed ~ = 90"
Viraria:
[
{attr: "temperature",op: "==",value:"40"},
{...}
]
I solved this problem using .split by passing the regular expression /(==|!=|>=|<=|~=|>|<)/
, but I'm not sure if the regular expression will execute in order, as I have >=
and <=
I put >
and <
at the end , so that it was not captured without analyzing all the alternatives of the expression.
var exprA = /(==|!=|>=|<=|~=|>|<)/;
var exprB = /(==|!=|~=|>|<|>=|<=)/;
var q = "temperature==40;engine!=fail;speed>90;speed<90;speed>=90;speed<=90;speed~=90";
var opts = q.split(';');
console.info('Expr. A');
for (var i in opts) {
var parts = (opts[i] || "").split(exprA);
console.log(parts.join(' '));
}
console.info('Expr. B:');
for (var i in opts) {
var parts = (opts[i] || "").split(exprB);
console.log(parts.join(' '));
}
See in the example above, in "expression B" the result was returned first to
>
and not >=
, which did not occur in expression A, the theory is confirmed but I'm not sure.
Is there any RFC documentation that confirms that regular expressions execute their options in order in all situations, uses and programming languages?