关于Safari浏览器在ios<16.3版本不支持正则表达式零宽断言的解决办法

异常原因

今天在升级Dify版本的时候发现低版本的ios手机出现了以下报错:

复制代码
SyntaxError: Invalid regular expression: invalid group specifier name

Error: Invalid regular expression: invalid group specifier name
Call Stack
46
eval
[native code] (0:0)
./node_modules/.pnpm/mdast-util-gfm-autolink-literal@2.0.1/node_modules/mdast-util-gfm-autolink-literal/lib/index.js

经过排查发现是Safari浏览器在ios手机低于16.3版本的时候不支持正则表达式的零宽断言。

参考原因:

  1. 相关链接:git issues
    3.相关链接: ios safari 正则兼容问题

解决方式

排查我的异常报错日志,是引用的一个包mdast-util-gfm-autolink-literal/造成的

复制代码
./node_modules/.pnpm/mdast-util-gfm-autolink-literal@2.0.1/node_modules/mdast-util-gfm-autolink-literal/lib/index.js

去git找相关的issues发现有人反馈过:

地址:iOS 和 Safari 版本 <16.4 中新的"负向后视"正则表达式中断

但是官方在此版本并不打算兼容低版本的标准,给出的建议也是使用Babel转译

所以到此有2个解决方案。

方案一:Babel转译配置

1. Babel转译配置

添加Babel插件以转换命名捕获组:

复制代码
npm install --save-dev @babel/plugin-transform-named-capturing-groups-regex

在babel.config.js中:

复制代码
module.exports = {
  plugins: ['@babel/plugin-transform-named-capturing-groups-regex']
};
2. 强制转译node_modules中的依赖

修改构建配置,确保相关依赖被Babel处理:

复制代码
// webpack.config.js 或类似配置
module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        include: /node_modules\/mdast-util-gfm-autolink-literal/,
        use: {
          loader: 'babel-loader',
          options: {
            plugins: ['@babel/plugin-transform-named-capturing-groups-regex']
          }
        }
      }
    ]
  }
};

重新编译

方案二:降级

mdast-util-gfm-autolink-literal在2.0.0的时候没有这个问题,如果可以选择降级的可以指定2.0.0版本

执行降级命令

复制代码
pnpm install mdast-util-gfm-autolink-literal@2.0.0

重新启动验证。

我使用降级后就不再报错了。

相关推荐
Digitally1 天前
如何在iPhone上共享日历
ios·iphone
库奇噜啦呼1 天前
【iOS】多线程学习
macos·ios·cocoa
xiAo_Ju1 天前
iOS一个Fancy UI的Tricky实现
前端·ios
游戏开发爱好者81 天前
iOS 商店上架全流程解析 从工程准备到审核通过的系统化实践指南
android·macos·ios·小程序·uni-app·cocoa·iphone
QuantumLeap丶1 天前
《Flutter全栈开发实战指南:从零到高级》- 18 -自定义绘制与画布
android·flutter·ios
狮智先生1 天前
【编程实践】正则表达式的使用
正则表达式
玲珑Felone1 天前
flutter 状态管理--InheritedWidget、Provider原理解析
android·flutter·ios
AskHarries2 天前
中国身份证注册美区 Apple Developer 个人账号完整教程
ios·apple
2501_916008892 天前
Objective-C 测试(OC 测试)指南 从单元测试到性能调优的多工具协同方法
android·ios·小程序·https·uni-app·iphone·webview