ITeye博客

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

jayzotion · 2009-05-20 09:46 · php · Web前端 · Markdown

需求如下:

  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)">

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。