`
jayzotion
  • 浏览: 47917 次
  • 性别: Icon_minigender_1
  • 来自: 森林之城
社区版块
存档分类
最新评论

工作需要写了一个匹配近似四则运算的正则表达式

    博客分类:
  • php
阅读更多
需求如下:
1. 匹配[!一定要求的字符串,数字,数字!]
2. 在 "[!!]"两边可有可无四则运算符,且可有可无 "()"
3. 在 "[!!]" 中间不能有连续的 "," 号
<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)">
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics