Can I use... Support tables for HTML5, CSS3, etc
这个网站是可以查询CSS、JS在浏览器各个版本中的兼容性
今天遇到一个String.replaceAll()报错的问题,在我电脑上正常,换了一个电脑后就报错,一开始以为是代码问题,后来看见了一篇文章发现是浏览器版本导致的。
replaceAll不支持一些低版本的浏览器,如果在开发中不能确保浏览器的版本,尽量使用其他的方案替代replaceAll
解决方案
1. 利用splite()
和join()
let str = '2021.01.25'//要将其转换成2021-01-25
console.log(str.split('.').join('-'))//2021-01-25
2. replace()
let str = '2021.01.25'//要将其转换成2021-01-25
console.log(str.replace(/\./g,'-'))//2021-01-25
replace的兼容性比replaceAll好。replace()接受两个参数,第一个参数是需要替换的字符,支持正则,如果不增加正则的 g 关键字,则表示提换第一个匹配的字符串。