我试图将值传递为95%
numexu = 95%
"^((>|GT|>=|GE|<|LT|<=|LE|==|EQ|!=|NE)?\\s*\\d?[%]?)$
if (!regex.IsMatch(numexu))
throw new ArgumentException("Percent expression is in an invalid format.");
它在代码中抛出异常。
此致 正则表达式
答案 0 :(得分:2)
您只检查1个号码\\d?
,请改为:\\d{0,2}
,这会接受0,1或2个号码。 ?
使其匹配为0或1次。
我不确定你是否需要逃离%
,如果是,那么\\%
。此外,如果您只有一个字符,则可以跳过括号[%]
,所以%
(或\\%
,如果需要转义)
答案 1 :(得分:0)
This Function will work for your requirement
function check() {
var txtfield; txtfield =document.getElementById('txtbox').value;
var reg=/^(\d{0,2}%?$)/;
if(reg.test(txtfield)){
alert("match");
}
else { alert("Try again"); }
}