• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

自定义公式校验规则、常用的JavaScript正则公式合法校验、自定义公式合集、前端校验规则、字符串校验

武飞扬头像
a_dream(前端)
帮助1

vue开发中,会使用到自定义公式校验合法性,判断公式是否符合逻辑, 整理个人使用过的自定义公式页面保存时对输入的字符串进行校验的一套规则

(文章最后有完整代码)

目录

1. 正则判断

2. 校验数字(输入数字不超过十位数,不超过两位小数)

3. 校验括号(嵌套括号是否符合要求)

4. (完整代码文件)


1. 正则判断

  1.  
    const re = /[^0-9\(\)\ \-\×\÷\.\#\{\}]{1,}/ // 判断输入字符合法性的正则(只能输入数字, -×÷,(),#{})
  2.  
    const re = /#\{(. ?)\}/g // 取出#{}中的字符正则
  3.  
    const re = /\((. ?)\)/g // 匹配出所有括号,返回数组
  4.  
     
  5.  
    if (/\(\)/.test(str)) throw new Error('括号中至少维护一个运算公式')
  6.  
     
  7.  
    // 错误情况,( 后面是运算符
  8.  
    if (/\([\ \-\×\÷]/.test(str)) { throw new Error('运算公式配置不正确') }
  9.  
    // 错误情况,) 前面是运算符
  10.  
    if (/[\ \-\×\÷]\)/.test(str)) { throw new Error('运算公式配置不正确') }
  11.  
    // 错误情况,( 前面不是运算符 或 空
  12.  
    if (/[^\ \-\×\÷\(\s]\(/.test(str)) { throw new Error('运算公式配置不正确') }
  13.  
    // 错误情况,) 后面不是运算符 或 空
  14.  
    if (/\)[^\ \-\×\÷\)\s]/.test(str)) { throw new Error('运算公式配置不正确') }
  15.  
    // 错误情况,运算符号不能在首末位
  16.  
    if (/^[\ \-\×\÷.]|[\ \-\×\÷.]$/.test(str)) { throw new Error('运算公式配置不正确') }
  17.  
    // 错误情况,运算符连续
  18.  
    if (/[\ \-\*\/]{2,}/.test(str)) { throw new Error('运算公式配置不正确') }
  19.  
    // 错误情况,#{}后面不是 运算符或 ) 或 ''
  20.  
    if (/#\{. \}[^\ \-\×\÷\)\s]/.test(str)) throw new Error('运算公式配置不正确')
  21.  
    // 错误情况,#{}前面不是 运算符或 ( 或 ''
  22.  
    if (/[^\ \-\×\÷\(\s]#\{. \}/.test(str)) throw new Error('运算公式配置不正确')
学新通

2. 校验数字(输入数字不超过十位数,不超过两位小数)

  1.  
    // 错误情况,.后面不是数字
  2.  
    if (/\.[^0-9]/.test(str)) throw new Error('公式不合法')
  3.  
    // 错误情况,.前面不是数字
  4.  
    if (/[^0-9]\./.test(str)) throw new Error('公式不合法')
  5.  
    // 判断数字是否超过10位,小数点是否超过两位
  6.  
    const arrNum = []
  7.  
    let tmp = ''
  8.  
    for (var i = 0; i < str.length; i ) {
  9.  
    if (
  10.  
    (formula.charAt(i) <= '9' && formula.charAt(i) >= '0') ||
  11.  
    formula.charAt(i) == '.'
  12.  
    ) {
  13.  
    tmp = formula.charAt(i)
  14.  
    } else {
  15.  
    if (tmp) {
  16.  
    arrNum.push(tmp)
  17.  
    tmp = ''
  18.  
    }
  19.  
    }
  20.  
    } // 在循环外加一个判断,是因为当字符串遍历到最后一位9的时候,不会走else里面的内容,无法加到数组
  21.  
    if (tmp) {
  22.  
    arrNum.push(tmp)
  23.  
    tmp = ''
  24.  
    }
  25.  
    arrNum.forEach((item) => {
  26.  
    if (parseInt(item) > 999999999) throw new Error('最大不超过十位数')
  27.  
    if (item.split('.')[1] && item.split('.')[1].length > 2) { throw new Error('最多输入两位小数') }
  28.  
    })
学新通

3. 校验括号(嵌套括号是否符合要求)

  1.  
    const left = [] // 遇到左括号存入数量和索引
  2.  
    const right = [] // 遇到右括号存入数量和索引
  3.  
    let lnum = 1 // 记录左括号的数量
  4.  
    let rnum = 1 // 记录右括号的数量
  5.  
    const str2 = str.split('') // 将字符串转为数组
  6.  
    for (let iii = 0; iii < str2.length; iii ) {
  7.  
    if (str2[iii] == '(') { // 循环遇到左括号
  8.  
    left.push({ num: lnum , index: iii }) // 存入数量和索引
  9.  
    } else if (str2[iii] == ')') { // 循环遇到右括号
  10.  
    right.push({ num: rnum , index: iii }) // 存入数量和索引
  11.  
    const xyz = str2.slice(left[left.length - 1].index, right[right.length - 1].index 1) // 取出遇到右括号时与上一个左括号之间的内容
  12.  
    // 校验xyz 判断括号中是否包含+- × ÷
  13.  
    const b = xyz.join('').match(regex1) // 将数组转回字符串进行取括号中的内容
  14.  
    if (b) {
  15.  
    // 判断字符串中是否有括号
  16.  
    for (let i = 0; i < b.length; i ) {
  17.  
    if (b[i].match(/×|-|[÷]|[ ]/)) {
  18.  
    // 匹配加减乘除
  19.  
    // console.log('有加减乘除')
  20.  
    } else {
  21.  
    // console.log('没有加减乘除')
  22.  
    throw new Error('括号中至少维护一个运算公式')
  23.  
    }
  24.  
    }
  25.  
    } else {
  26.  
    console.log('没有括号')
  27.  
    }
  28.  
    // 删除匹配完后两个位置的括号,替换成空格
  29.  
    str2.splice(left[left.length - 1].index, 1, ' ')
  30.  
    str2.splice(right[right.length - 1].index, 1, ' ')
  31.  
    // 删除数组中最后一项,继续匹配
  32.  
    left.pop()
  33.  
    right.pop()
  34.  
    }
  35.  
    }
学新通

4. (完整代码文件)

  1.  
    const re = /[^0-9\(\)\ \-\×\÷\.\#\{\}]{1,}/ // 判断输入字符合法性的正则(只能输入数字, -×÷,(),#{})
  2.  
    const ru = /#\{(. ?)\}/g // 取出#{}中的字符正则
  3.  
    const regex1 = /\((. ?)\)/g // 匹配出所有括号,返回数组
  4.  
    // formula为传入需要校验的字符串; ruleArr为定义的系统提供的字段数组(#{}包裹的内容)
  5.  
    export const validatedFunc = function(formula, ruleArr) {
  6.  
    // 剔除空白符
  7.  
    formula = formula.replace(/\s/g, '')
  8.  
    // 如果输入了不合法的字符,直接返回不合法(除去#{}中的内容)
  9.  
    const str = formula.replace(/#\{(. ?)\}/g, '')
  10.  
    let result = false
  11.  
    result = re.test(str)
  12.  
    if (result) throw new Error('请选择系统提供的字段和运算符')
  13.  
    // 空括号
  14.  
    if (/\(\)/.test(formula)) throw new Error('括号中至少维护一个运算公式')
  15.  
    const left = [] // 遇到左括号存入数量和索引
  16.  
    const right = [] // 遇到右括号存入数量和索引
  17.  
    let lnum = 1 // 记录左括号的数量
  18.  
    let rnum = 1 // 记录右括号的数量
  19.  
    const str2 = formula.split('') // 将字符串转为数组
  20.  
    for (let iii = 0; iii < str2.length; iii ) {
  21.  
    if (str2[iii] == '(') { // 循环遇到左括号
  22.  
    left.push({ num: lnum , index: iii }) // 存入数量和索引
  23.  
    } else if (str2[iii] == ')') { // 循环遇到右括号
  24.  
    right.push({ num: rnum , index: iii }) // 存入数量和索引
  25.  
    const xyz = str2.slice(left[left.length - 1].index, right[right.length - 1].index 1) // 取出遇到右括号时与上一个左括号之间的内容
  26.  
    // 校验xyz
  27.  
    // 判断括号中是否包含+- × ÷
  28.  
    const b = xyz.join('').match(regex1) // 将数组转回字符串进行取括号中的内容
  29.  
    if (b) {
  30.  
    // 判断字符串中是否有括号
  31.  
    for (let i = 0; i < b.length; i ) {
  32.  
    if (b[i].match(/×|-|[÷]|[ ]/)) {
  33.  
    // 匹配加减乘除
  34.  
    // console.log('有加减乘除')
  35.  
    } else {
  36.  
    // console.log('没有加减乘除')
  37.  
    throw new Error('括号中至少维护一个运算公式')
  38.  
    }
  39.  
    }
  40.  
    } else {
  41.  
    console.log('没有括号')
  42.  
    }
  43.  
    // 删除匹配完后两个位置的括号,替换成空格
  44.  
    str2.splice(left[left.length - 1].index, 1, ' ')
  45.  
    str2.splice(right[right.length - 1].index, 1, ' ')
  46.  
    // 删除数组中最后一项,继续匹配
  47.  
    left.pop()
  48.  
    right.pop()
  49.  
    }
  50.  
    }
  51.  
    // 判断数字相关
  52.  
    // 错误情况,.后面不是数字
  53.  
    if (/\.[^0-9]/.test(formula)) throw new Error('公式不合法')
  54.  
    // 错误情况,.前面不是数字
  55.  
    if (/[^0-9]\./.test(formula)) throw new Error('公式不合法')
  56.  
    // 判断数字是否超过10位,小数点是否超过两位
  57.  
    const arrNum = []
  58.  
    let tmp = ''
  59.  
    for (var i = 0; i < formula.length; i ) {
  60.  
    if (
  61.  
    (formula.charAt(i) <= '9' && formula.charAt(i) >= '0') ||
  62.  
    formula.charAt(i) == '.'
  63.  
    ) {
  64.  
    tmp = formula.charAt(i)
  65.  
    } else {
  66.  
    if (tmp) {
  67.  
    arrNum.push(tmp)
  68.  
    tmp = ''
  69.  
    }
  70.  
    }
  71.  
    } // 在循环外加一个判断,是因为当字符串遍历到最后一位9的时候,不会走else里面的内容,无法加到数组
  72.  
    if (tmp) {
  73.  
    arrNum.push(tmp)
  74.  
    tmp = ''
  75.  
    }
  76.  
    arrNum.forEach((item) => {
  77.  
    if (parseInt(item) > 999999999) throw new Error('最大不超过十位数')
  78.  
    if (item.split('.')[1] && item.split('.')[1].length > 2) { throw new Error('最多输入两位小数') }
  79.  
    })
  80.  
    // 判断括号相关
  81.  
    // 括号不配对
  82.  
    const stack = []
  83.  
    for (var q = 0, item; q < formula.length; q ) {
  84.  
    item = formula.charAt(q)
  85.  
    if (item === '(') {
  86.  
    stack.push('(')
  87.  
    } else if (item === ')') {
  88.  
    if (stack.length > 0) {
  89.  
    stack.pop()
  90.  
    } else {
  91.  
    return false
  92.  
    }
  93.  
    }
  94.  
    }
  95.  
    if (stack.length !== 0) throw new Error('括号必须成对出现')
  96.  
    // 错误情况,( 后面是运算符
  97.  
    if (/\([\ \-\×\÷]/.test(formula)) { throw new Error('运算公式配置不正确') }
  98.  
    // 错误情况,) 前面是运算符
  99.  
    if (/[\ \-\×\÷]\)/.test(formula)) { throw new Error('运算公式配置不正确') }
  100.  
    // 错误情况,( 前面不是运算符 或 空
  101.  
    if (/[^\ \-\×\÷\(\s]\(/.test(formula)) { throw new Error('运算公式配置不正确') }
  102.  
    // 错误情况,) 后面不是运算符 或 空
  103.  
    if (/\)[^\ \-\×\÷\)\s]/.test(formula)) { throw new Error('运算公式配置不正确') }
  104.  
    // 错误情况,运算符号不能在首末位
  105.  
    if (/^[\ \-\×\÷.]|[\ \-\×\÷.]$/.test(formula)) { throw new Error('运算公式配置不正确') }
  106.  
    // 错误情况,运算符连续
  107.  
    if (/[\ \-\*\/]{2,}/.test(formula)) { throw new Error('运算公式配置不正确') }
  108.  
    // 错误情况,#{}后面不是 运算符或 ) 或 ''
  109.  
    // console.log(/#\{. \}[^\ \-\×\÷]/.test(formula), '.......................')
  110.  
    if (/#\{. \}[^\ \-\×\÷\)\s]/.test(formula)) throw new Error('运算公式配置不正确')
  111.  
    // 错误情况,#{}前面不是 运算符或 ( 或 ''
  112.  
    if (/[^\ \-\×\÷\(\s]#\{. \}/.test(formula)) throw new Error('运算公式配置不正确')
  113.  
    // 判断不是系统提供的字段(#{}包含的内容)
  114.  
    const e = formula.match(ru)
  115.  
    if (e) {
  116.  
    e.forEach(item => {
  117.  
    const o = item.match(/#\{(. )\}/)[1]
  118.  
    // console.log(item.match(/#\{(. )\}/)[1])
  119.  
    const dd = ruleArr.filter(val => o == val.ruleName)
  120.  
    // console.log(dd, 'dddddddddddddddddddd')
  121.  
    if (!dd.length) throw new Error('请选择系统提供的字段')
  122.  
    })
  123.  
    }
  124.  
    return true
  125.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgejehj
系列文章
更多 icon
同类精品
更多 icon
继续加载