工作需要写了一个匹配近似四则运算的正则表达式
需求如下:
- 匹配
- 在 ""两边可有可无四则运算符,且可有可无 "()"
- 在 "" 中间不能有连续的 "," 号
<SCRIPT type="text/javaScript">
<!--
// if match return true
//else return false;
function isMatch(str){
if(str.match(/^(^([\(\)]{0,}\[\![0-9a-za-z\_\-]{1,}\,\d{1}\,\d{1,}\!\][\)\(]{0,}[\+\-\*\/\%]{0,}){1,}
([\(\)]{0,}\[\![0-9a-za-z\_\-]{1,}\,\d{1,}\,\d{1,}\!\][\(\)]{0,}){1,}$)*$/g)
|| str.match(/^(\[\![0-9a-za-z\_\-]{1,}\,\d{1}\,\d{1,}\!\])$/g) ){
return true;
}
return false;
}
//the function of test
function test(inputStr) {
if(true == isMatch(inputStr)){
alert(1);
}else{
alert(0);
}
}
//-->
</SCRIPT>
<!--测试-->
<input type = "text" id = "test" onchange ="test(this.value)">