低版本浏览器不兼容relaceAll 用正则+replace替换 relaceAll is not a function 解决办法
低版本浏览器不兼容relaceAll 用正则+replace替换
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>低版本浏览器不兼容relaceAll 用正则+replace替换</title> </head> <body> <script type="text/javascript"> const str = 'foo-bar'; // 这种方法2 const re = new RegExp('foo','g'); const result1 = str.replace(str.match(re),'moo'); // 正则方法2 const result2 = str.replace(/foo/g, 'moo'); // output: 'moo-bar' console.log(result1); console.log(result2); </script> </body> </html>