【问题标题】:How to validate infix notation in javascript?如何在javascript中验证中缀符号?
【发布时间】:2013-12-30 05:32:18
【问题描述】:

我有一个中缀表达式:((attribute1*attribute2)/attribute3+attribute4)

可能会因用户输入而异。我想检查表达式是否有效。

有效示例:((attribute1*attribute2)/attribute3+attribute4)

无效示例:(attrribute1*attribute2+*(attribute3)

第二个没有右括号;也不需要* 运算符。如何在 javascript 中执行这种验证?

现在这是我的正则表达式:

/ *\+? *\-? *[a-zA-Z0-9]+ *( *[\+\-\*\/\=\<\>\!\&\|\%] *\+? *\-? *[a-zA-Z0-9]+ *)*/

我需要一个用于比较运算符的正则表达式,如 &lt;=&gt;=!=== 等。我该如何实现?

【问题讨论】:

    标签: javascript validation expression comparison-operators infix-notation


    【解决方案1】:

    你可以试试这样的:

    function validateInfix(infix) {
        var balance = 0;
        // remove white spaces to simplify regex
        infix = infix.replace(/\s/g, '');
    
        // if it has empty parenthesis then is not valid
        if (/\(\)/.test(infix)) {
            return false;
        }
    
        // valid values: integers and identifiers
        var value = '(\\d+|[a-zA-Z_]\\w*)';
        // the unary '+' and '-'
        var unaryOper = '[\\+\\-]?';
        // the arithmetic operators
        var arithOper = '[\\+\\-\\*\\/]';
        // the comparison operators
        var compOper = '(\\<\\=?|\\>\\=?|\\=\\=|\\!\\=)';
    
        // if it has more than one comparison operator then is not valid
        if (infix.match(new RegExp(compOper, 'g')).length > 1) {
            return false;
        }
    
        // the combined final regex: /[\+\-]?(\d+|[a-zA-Z_]\w*)(([\+\-\*\/]|(\<\=?|\>\=?|\=\=|\!\=))[\+\-]?(\d+|[a-zA-Z_]\w*))*/
        var regex = new RegExp(unaryOper + value + '((' + arithOper + '|' + compOper + ')' + unaryOper + value + ')*');
    
        // validate parenthesis balance
        for (var i = 0; i < infix.length; i++) {
            if (infix[i] == '(') {
                balance++;
            }
            else if (infix[i] == ')') {
                balance--;
            }
    
            if (balance < 0) {
                return false;
            }
        }
    
        if (balance > 0) {
            return false;
        }
    
        // remove all the parenthesis
        infix = infix.replace(/[\(\)]/g, '');
    
        return regex.test(infix);
    }
    

    我们的想法是首先检查括号平衡,然后将它们全部删除,因为我们只想验证而不是评估,然后将剩余的表达式与正则表达式匹配(这可能不完美,我不是正则表达式专家)。而且...以防万一:infix 参数必须是字符串。

    编辑

    我注意到了一些细节并稍微更改了代码:

    1. 添加了您也需要正则表达式匹配的运算符。
    2. 删除了空格以消除正则表达式垃圾。
    3. 检查表达式是否有空括号。
    4. 检查表达式是否有多个比较运算符。
    5. 将此\+?\-? 更改为此[\+\-]?
    6. 尽可能将string match method 更改为regex test method
    7. 将此[a-zA-Z0-9] 更改为此(\d+|[a-zA-Z_]\w*),因为第一个匹配错误的标识符,例如53abc
    8. 为了更好地理解和清晰,将正则表达式提取到单独的变量中,并从中构建最后一个。

    希望你现在没问题 :)

    【讨论】:

    • 感谢 ecampver !现在这是我的正则表达式,var regex = / *\+? *\-? *[a-zA-Z0-9]+ *( *[\+\-\*\/\=\&lt;\&gt;\!\&amp;\|\%] *\+? *\-? *[a-zA-Z0-9]+ *)*/; 我需要一个用于比较运算符的正则表达式,例如 = 、 != 、 == 等?我该如何实现这个?
    • @sri:编辑了我的答案,检查一下,如果您发现任何其他问题,请询问;)
    • @sri:嗯?这个答案对你有帮助吗?请一些反馈会很好。
    猜你喜欢
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多