正则校验计算方式是否正确

60 阅读2分钟

正则校验输入的算法是否正确


function CalculationFormulas(string, obj) {
  // console.log(string);
  console.log(obj);

  // 剔除空白符
  string = string.replace(/\s/g, '');

  // 错误情况,空字符串
  if (string === "") {
    console.error("空字符串");
    return false;
  }

  // 错误情况,运算符开头
  if (/^[+\-*/x÷]/.test(string)) {
    console.error("运算符开头", string);
    return false;
  }

  // 错误情况,运算符结尾
  if (/[+\-*/x÷]$/.test(string)) {
    console.error("运算符结尾", string);
    return false;
  }

  // 是否输入了括号
  if (/[(){}\[\]]/g.test(string)) {

    // 空括号
    if (/\(\)/.test(string)) {
      console.error("空括号", string);
      return false;
    }

    // 错误情况,(后面是运算符
    if (/\([\+\-\*\/x÷]/.test(string)) {
      console.error("括号后面是运算符", string);
      return false;
    }

    // 错误情况,括号不配对
    // let stack = [];
    // for (let i = 0; i < string.length; i++) {
    //   let char = string.charAt(i);
    //   if (char === '(') {
    //     stack.push('(');
    //   } else if (char === ')') {
    //     if (stack.length > 0) {
    //       stack.pop();
    //     } else {
    //       console.error("括号不配对", string);
    //       return false;
    //     }
    //   }
    // }
    if (!checkBrackets(string)) {
      console.error("括号不配对", string);
      return false;
    }


    // 错误情况,)前面是运算符
    if (/[\+\-\*\/x÷]\)/.test(string)) {
      console.error("运算符前括号", string);
      return false;
    }

    // 错误情况,(前面不是运算符
    if (!/[+\-*/x÷]\(/.test(string)) {
      console.error("括号后运算符", string);
      return false;
    }

    // 错误情况,)后面不是运算符
    if (/\)[^\+\-\*\/x÷)]/.test(string)) {
      console.error("括号后不是运算符", string);
      return false;
    }
  }

  // 错误情况,运算符连续
  if (/[\+\-\*\/x÷]{2,}/.test(string)) {
    console.error("运算符连续", string);
    return false;
  }

  // 错误情况,变量没有来自“待选公式变量”
  let tmpStr = string.replace(/[\(\)\+\-\*\/x÷]+/g, ',');
  let array = tmpStr.split(',');

  for (let i = 0; i < array.length; i++) {
    let item = array[i];
    if (/[A-Z]/i.test(item) && typeof obj[item] === 'undefined') {
      console.error("变量不在待选公式变量中:", item);
      return false;
    }
  }

  let stringarr = string.split('');
  let objarr = Object.keys(obj);

  for (let index = 0; index < stringarr.length; index++) {
    if (objarr.includes(stringarr[index])) {
      if (stringarr[index + 1] === undefined) {
        continue;
      } else if (!['+', '-', 'x', '÷', '*', '/', '(', ')'].includes(stringarr[index + 1])) {
        console.error("无效的符号:", stringarr[index + 1]);
        return false;
      }
    }
  }

  return true;
};


// 括号匹配法
function checkBrackets(string) {
  let stack = [];
  for (let i = 0; i < string.length; i++) {
    let char = string.charAt(i);
    if (char === '(' || char === '[' || char === '{') {
      stack.push(char);
    } else if (char === ')' || char === ']' || char === '}') {
      if (stack.length > 0) {
        let lastChar = stack.pop();
        if ((char === ')' && lastChar !== '(') ||
          (char === ']' && lastChar !== '[') ||
          (char === '}' && lastChar !== '{')) {
          console.error("括号不配对", string);
          return false;
        }
      } else {
        console.error("括号不配对", string);
        return false;
      }
    }
  }
  if (stack.length !== 0) {
    console.error("括号不配对", string);
    return false;
  }
  return true;
}

目前校验不是很完整,只能校验小括号的。如果要包括[] {} 这两种括号一起的话还是会返回不通过的。 后面有空再补。