简单计算公式校验
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('公式异常:括号个数不匹配') }