遇到问题

input框中输入全角空格,提交时,使用以下方法并没有被过滤,表单提交成功。

const removeSpace = function(str) {
  // 去除空格
  if (!str) {
    return ''
  } else {
    return str.replace(/\s/gi, '')
  }
}

解决方法

const removeSpace = function(str) {
  // 去除空格
  if (!str) {
    return ''
  } else {
    return str.replace(/(\s*)|(\s*$)/g, '')
  }
}

扩展--去除字符串首尾的空格

const removeSpace = function(str) {
  // 去除空格
  if (!str) {
    return ''
  } else {
    return str.replace(/(^\s*)|(\s*$)/g, '')
  }
}

相关文章:

  • 2022-12-23
  • 2022-02-14
  • 2021-08-09
  • 2022-12-23
  • 2022-12-23
  • 2021-10-02
  • 2022-12-23
  • 2021-10-04
猜你喜欢
  • 2022-12-23
  • 2021-06-26
  • 2022-12-23
  • 2021-07-19
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案