JS-数学表达式正则表达式支持(包含希腊字母、小数点等)
//技术状况规则 /** evt: { target:{ value:'' } }, row: { "propName":"" "propRule":"" } */ //技术状况规则 onRuleShow: function (evt, row, propName, propRule) { //匹配a=5,a>5,a<5,a≤6,a≥5等 var rule1 = /^[ΆΈ-ώa-zA-Z]+((\d+(\.\d+)?)|(\d+(\.\d+)?))*[≤<==>≥](((\-)?\d+(\.\d+)?)|(\d+(\.\d+)?))+$/; var rule1Left = /^[ΆΈ-ώa-zA-Z]+((\d+(\.\d+)?)|(\d+(\.\d+)?))*[≤<==>≥]/; var rule1Right = /[≤<==>≥](((\-)?\d+(\.\d+)?)|(\d+(\.\d+)?))+$/; //匹配5<p<10,5<p1<20,5≤p1<20,5<p1≤20等 var rule2 = /^(((\-)?\d+(\.\d+)?)|(\d+(\.\d+)?))+[≤<][ΆΈ-ώa-zA-Z]+((\d+(\.\d+)?)|(\d+(\.\d+)?))*[≤<](((\-)?\d+(\.\d+)?)|(\d+(\.\d+)?))+$/; var rule2Min = /^(((\-)?\d+(\.\d+)?)|(\d+(\.\d+)?))+[≤<]/; var rule2Max = /[≤<](((\-)?\d+(\.\d+)?)|(\d+(\.\d+)?))+$/; var rule2Mid = /[≤<][ΆΈ-ώa-zA-Z]+((\d+(\.\d+)?)|(\d+(\.\d+)?))*[≤<]/; var tv0 = evt.target.value.replace(/\s*/g, "").replace(/^\s*|\s*$/g, ""); var tv = tv0.replace("<", "<").replace(">", ">").replace("=","=").trim(); row[propName] = tv; var v1 = tv.match(rule1); var v2 = tv.match(rule2); if (v1 == null && v2 == null) { row[propRule] = ''; row[propName] = ''; return; } if (v2 != null && v2.length > 0) { var v2Min = v2[0].match(rule2Min); var v2Max = v2[0].match(rule2Max); var v2Mid = v2[0].match(rule2Mid); var leftChar = ''; var rightChar = ''; var leftCharNew = ''; var rightCharNew = ''; var midCharNew = ''; if (v2Min.length > 0) { //提取左边符号 leftChar = v2Min[0][v2Min[0].length - 1]; leftCharNew = v2Min[0].substr(0, v2Min[0].length - 1); } if (v2Max.length > 0) { //提取右边符号 rightChar = v2Max[0][0]; rightCharNew = v2Max[0].substr(1, v2Max[0].length - 1); } if (v2Mid.length > 0) { midCharNew = v2Mid[0].substr(1, v2Mid[0].length - 2); } if (leftChar == '≤') leftChar = '['; else if (leftChar == '<' || leftChar == '<') leftChar = '('; else leftChar = ''; if (rightChar == '≤') rightChar = ']'; else if (rightChar == '<' || rightChar == '<') rightChar = ')'; else rightChar = ''; if (leftChar.length == 0 || rightChar.length == 0) { row[propRule] = ''; row[propName] = ''; return; } var strNewRule = leftChar + parseFloat(leftCharNew) + ',' + parseFloat(rightCharNew) + rightChar; row[propRule] = strNewRule; row[propName] = v2[0]; return; } else if (v1 != null && v1.length > 0) { var v1Char = ''; var v1Left = v1[0].match(rule1Left); var v1Right = v1[0].match(rule1Right); if (v1Left.length > 0) { //提取右边符号 v1Char = v1Left[0][v1Left[0].length - 1]; //去除左边符号 v1Left = v1Left[0].substr(0, v1Left[0].length - 1); } if (v1Right.length > 0) { //去除左边符号 v1Right = v1Right[0].substr(1, v1Right[0].length - 1); } var strNewRule = ''; if (v1Char == '=' || v1Char == '=') { strNewRule = "[" + v1Right + "," + v1Right + "]"; } else if (v1Char == '<' || v1Char == '<') { strNewRule = v1Right + ")"; } else if (v1Char == '≤') { strNewRule = v1Right + "]"; } else if (v1Char == '>' || v1Char == '>') { strNewRule = "(" + v1Right; } else if (v1Char == '≥') { strNewRule = "[" + v1Right; } row[propRule] = strNewRule; row[propName] = v1[0]; } else { row[propName] = ''; row[propRule] = ''; return; } }
调用示例:
<el-table :data="props.row.Details" border :ref="'subTable'+props.$index" style="width:100%;" :span-method="rowSpanMethod" v-show="props.row.Details && props.row.Details.length>0"> <el-table-column label="计算规则"> <template slot-scope="prop"> <el-form-item :prop="'Items.' + props.$index + '.Details.' + prop.$index + '.EvalRuleValue'" :rules="[ {required: true, message: '计算规则不能为空', trigger: 'blur'} ]"> @*<el-input v-model="prop.row.EvalRuleValue" v-on:keyup.native="onNumber($event,prop.row,'EvalRuleValue')" min="0" max="10000">*@ <el-input v-model="prop.row.EvalRuleValue" v-bind:disabled="!isDisabled"> </el-input> </el-form-item> </template> </el-table-column> <el-table-column label="规则展示"> <template slot-scope="prop"> <el-form-item :prop="'Items.' + props.$index + '.Details.' + prop.$index + '.EvalRuleName'" :rules="[ {required: true, message: '规则展示不能为空', trigger: 'blur'} ]"> @*<el-input v-model="prop.row.EvalRuleName" v-on:keyup.native="onRuleShow($event,prop.row,'EvalRuleName','EvalRuleValue')">*@ <el-input v-model="prop.row.EvalRuleName" v-on:blur.capture="onRuleShow($event,prop.row,'EvalRuleName','EvalRuleValue')"></el-input> </el-form-item> </template> </el-table-column> </el-table>
示例效果: