如何在javascript中验证中缀符号?
我有一个中缀表达式:如何在javascript中验证中缀符号?,javascript,validation,expression,comparison-operators,infix-notation,Javascript,Validation,Expression,Comparison Operators,Infix Notation,我有一个中缀表达式:((attribute1*attribute2)/attribute3+attribute4) 它可能根据用户输入而变化。我想检查表达式是否有效 有效示例:((attribute1*attribute2)/attribute3+attribute4) 无效示例:(属性1*attribute2+*(属性3) 第二个没有右括号;也不需要*运算符。如何在javascript中执行这种验证 这是我的正则表达式: / *\+? *\-? *[a-zA-Z0-9]+ *( *[\+\-\
((attribute1*attribute2)/attribute3+attribute4)
它可能根据用户输入而变化。我想检查表达式是否有效
有效示例:((attribute1*attribute2)/attribute3+attribute4)
无效示例:(属性1*attribute2+*(属性3)
第二个没有右括号;也不需要*
运算符。如何在javascript中执行这种验证
这是我的正则表达式:
/ *\+? *\-? *[a-zA-Z0-9]+ *( *[\+\-\*\/\=\<\>\!\&\|\%] *\+? *\-? *[a-zA-Z0-9]+ *)*/
/*\+?*-?*[a-zA-Z0-9]+*(*[\+-\*\/\=\!\&\\\%]*\+?*-?*[a-zA-Z0-9]+*)*/
我需要一个用于比较运算符的正则表达式,如
=
、!=
、=
等。我如何实现这一点?您可以尝试以下方法:
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);
}
函数validateInfix(中缀){
var余额=0;
//删除空白以简化正则表达式
中缀=中缀。替换(/\s/g');
//如果有空括号,则无效
if(/\(\)/.test(中缀)){
返回false;
}
//有效值:整数和标识符
变量值='(\\d+|[a-zA-Z|]\\w*);
//一元“+”和“-”
变量unaryOper='[\\+\-]?';
//算术运算符
var arithOper='[\\+\-\\*\\/]';
//比较运算符
变量组合='(\\\=?\\=\\=\\=\\=\\\\\\!\=);
//如果它有多个比较运算符,则无效
if(infix.match(新的RegExp(compOper,'g')).length>1){
返回false;
}
//组合的最终正则表达式:/[\+\-]?(\d+\[a-zA-Z]\w*)(([\+\-\*\/]。(\\=?\\=\=\=\=\=\!\=)[\+\-]?(\d+\[a-zA-Z]\w*))*/
var regex=new RegExp(unaryOper+value+'('+arithOper+'|'+compOper+')'+unaryOper+value+')*');
//验证括号平衡
对于(变量i=0;i0){
返回false;
}
//删除所有括号
中缀=中缀。替换(/[\(\)]/g',);
返回正则表达式测试(中缀);
}
我们的想法是首先检查括号的平衡,然后在我们只想验证而不想求值的情况下删除所有括号,然后将剩余的表达式与正则表达式匹配(这可能不完美,我不是正则表达式专家)。并且…以防万一:中缀
参数必须是字符串
编辑
我注意到一些细节,并稍微更改了代码:
[\+\-]?
更改此\+?\-?
(\d+|[a-zA-Z]\w*)
更改了此[a-zA-Z0-9]
,因为第一个匹配错误的标识符,如53abc
希望您现在可以使用:)您可以尝试以下方法:
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);
}
函数validateInfix(中缀){
var余额=0;
//删除空白以简化正则表达式
中缀=中缀。替换(/\s/g');
//如果有空括号,则无效
if(/\(\)/.test(中缀)){
返回false;
}
//有效值:整数和标识符
变量值='(\\d+|[a-zA-Z|]\\w*);
//一元“+”和“-”
变量unaryOper='[\\+\-]?';
//算术运算符
var arithOper='[\\+\-\\*\\/]';
//比较运算符
变量组合='(\\\=?\\=\\=\\=\\=\\\\\\!\=);
//如果它有多个比较运算符,则无效
if(infix.match(新的RegExp(compOper,'g')).length>1){
返回false;
}
//组合的最终正则表达式:/[\+\-]?(\d+\[a-zA-Z]\w*)(([\+\-\*\/]。(\\=?\\=\=\=\=\=\!\=)[\+\-]?(\d+\[a-zA-Z]\w*))*/
var regex=new RegExp(unaryOper+value+'('+arithOper+'|'+compOper+')'+unaryOper+value+')*');
//验证括号平衡
对于(变量i=0;i0){
返回false;
}
//删除所有括号
中缀=中缀。替换(/[\(\)]/g',);
返回正则表达式测试(中缀);
}
我们的想法是首先检查括号的平衡,然后删除所有括号,因为我们只想验证而不想求值,然后将剩余的表达式与正则表达式匹配(这可能并不完美,我不是正则表达式专家)。而且。。。以防万一:infix
参数必须是字符串
编辑
我注意到一些细节,并稍微更改了代码:
[\+\-]?
更改此\+?\-?
(\d+|[a-zA-Z]\w*)
更改了此[a-zA-Z0-9]
,因为第一个匹配错误的标识符,如53abc
希望您现在一切顺利:)谢谢!这是我的正则表达式,
var regex=/*\+?*\-*[a-zA-Z0-9]+*(*[\+\-\*\/\=\!\&\\\%]*\+?*-?*[a-zA-Z0-9]+*)*/代码>我需要一个正则表达式,用于比较运算符,如=,