let formulaStr = '你的公式' if(/^[\d ()*\/+-]+$/.test(formulaStr)){}else { console.log('公式异常:包含不能解析的字符') } if(/[\+\-\*\/]{2,}/.test(formulaStr)){ console.log('公式异常:运算符连续') } if(/\(\)/.test(formulaStr)){ console.log('公式异常:空括号') } if(/\([\+\-\*\/]/.test(formulaStr)){ console.log('公式异常:(后面是运算符') } if(/[\+\-\*\/]\)/.test(formulaStr)){ console.log('公式异常:)前面是运算符') } if(/^[\+\-\*\/.]|[\+\-\*\/.]$/.test(formulaStr)){ console.log('公式异常:运算符号不能在首末位') } let rea = formulaStr.match(/\(/g); let reb = formulaStr.match(/\)/g); let reaLen = !rea ? 0 : rea.length; let rebLen = !reb ? 0 : reb.length; if(reaLen !== rebLen){ console.log('公式异常:括号个数不匹配') }
标签:console,log,公式,校验,let,formulaStr,简单,test,计算公式 From: https://www.cnblogs.com/zhaozhou/p/17638293.html