---
title: "工作需要写了一个匹配近似四则运算的正则表达式"
author: "jayzotion"
date: "2009-05-20T09:46:14Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2009-05-20T09:54:16Z"
id: 391568
url: "https://jayzotion.iteye.com/blog/391568"
---

需求如下:
1. 匹配
2. 在 ""两边可有可无四则运算符,且可有可无 "()"
3. 在 "" 中间不能有连续的 "," 号

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

```