特殊字符串处理, 将字符串中的数字过滤出来

50 阅读1分钟

前言: 最近项目输入框的需求, input框选择数据后显示类似"1-10万", "10-20万"这样的数据, 然后我们需要处理数据拿到最小值, 最大值,向后端请求数据

    let a = '1-10万'
    let b = a.match(/\d+/g)
    console.log(b) // ['1', '10']
  let a = '1-10个'
  
  let min = a.match(/\d+/g) && a.match(/\d+/g).length ? a.match(/\d+/g)[0] : ''
  let max = a.match(/\d+/g) && a.match(/\d+/g).length > 1 ? a.match(/\d+/g)[1] : ''