Reputation: 352
I'm trying to write something like eval(), here are some of the requirements:
if the expression contains non-numeric character other than [+, -, *, /, .],
or [+, -] is followed by [*, /],
or [+, -, *, /] is not proceeded by a numeric e.g."10-",
or [*, /] is not preceded by a numeric e.g."-10" is valid, "*10" is not valid
throw an exception
I have the following code:
if (expression.match(/[^+\-*/\d.]/) ||
expression.match(/[+-](?=[*/])/) ||
expression.match(/[+\-*/]($|[^+\-.\d])/) ||
expression.match(/(^|[^+\-.\d])[*/]/))
throw errors.ExpressionParserError;
it works fine when the expressions are separated as above, but when I combine them with |(OR), it doesn't throw an exception anymore
if (expression.match(/[^+\-*/\d.] | [+-](?=[*/]) | [+\-*/]($|[^+\-.\d]) | (^|[^+\-.\d])[*/]/))
throw errors.ExpressionParserError;
e.g. "d * 1" should fall in the first part of the expression [^+-*/\d.]
What did I miss here? Thanks.
Upvotes: 0
Views: 149
Reputation: 1074385
Two things:
|
. Those spaces are significant.(?:...)
).So for instance:
if (expression.match(/(?:[^+\-*/\d.])|(?:[+-](?=[*/]))|(?:[+\-*/]($|[^+\-.\d]))|(?:(^|[^+\-.\d])[*/])/))
// -------------------^^^-----------^^^--------------^^^----------------------^^^-------------------^
throw errors.ExpressionParserError;
Side note: To just test for a match, use rex.test(str)
rather than str.match(rex)
. No need to build a result array if your only goal is to test for a match.
Upvotes: 1