项目中文件上传需要验证文件的格式,第一次正常,第二次就验证不通过了。在验证的地方console.log()两遍,发现结果不一样 !!!

正则和文件名都没变,但是两次的验证结果不同。

this.reg = /\.(xlsx|xls)$/ig;
console.log(this.reg.test(newFile.name)) //true
console.log(this.reg.test(newFile.name)) //false

 

lastIndex 这一属性用于匹配下次检索的开始位置,该属性可读写,exec() 或 test()方法可以改写这一属性。

当找不到匹配内容是,lastIndex被自动设为0。

解决方案

第一种方案是将g去掉,关闭全局匹配。

第二种就是在每次匹配之前将lastIndex的值设置为0。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-02
  • 2022-12-23
  • 2021-12-05
猜你喜欢
  • 2021-11-18
  • 2021-11-19
  • 2021-07-04
  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案