You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome 95中出现正则表达式过大语法错误的解决求助

解决Chrome 95+中"Regular expression too large"错误的方案

看起来你遇到的是Chrome 95版本V8引擎引入的特殊行为(或者说bug)——尽管你的正则表达式本身非常短小,但引擎在某些场景下会误判它"过大",尤其是当正则应用在window.location.origin这类字符串上时。下面是几个可行的修复方案:

方案一:完全避开正则表达式,直接操作字符串

这种方法最稳妥,能彻底绕过引擎的正则处理问题,同时保持所有浏览器的兼容性:

export const baseURL = (() => {
  let origin = window.location.origin || 'https://www.xxxxx.com';
  
  // 移除端口号部分
  if (origin.includes(':')) {
    const lastColonIndex = origin.lastIndexOf(':');
    // 确认冒号后面是数字端口,再移除
    const portPart = origin.slice(lastColonIndex + 1);
    if (!isNaN(Number(portPart)) && portPart.length > 0) {
      origin = origin.slice(0, lastColonIndex);
    }
  }

  // 替换子域名
  const [protocol, hostPart] = origin.split('://');
  const newHost = hostPart.replace(/^(\S+?)\./, () => 
    `${window.station === 'cn' ? 'aaaa' : 'bbbb'}.`
  );

  return `${protocol}://${newHost}`;
})();

方案二:简化正则匹配逻辑(如果坚持用正则)

如果一定要用正则,可以调整正则的写法,让它更精准,降低引擎误判的概率:

export const baseURL = (window.location.origin || 'https://www.xxxxx.com')
  // 端口号最多5位(合法范围1-65535),用固定长度限制替代模糊匹配
  .replace(/:\d{1,5}$/, '')
  .replace(/\/\/(\S+?)\./, (_a, _p1) => `//${window.station === 'cn' ? 'aaaa' : 'bbbb'}.`);

额外排查步骤

在修复前可以先在Chrome 95+的控制台打印window.location.origin的值,确认它没有被注入超长内容(比如恶意脚本或异常页面状态导致origin变得异常长)——这种极端情况也可能触发引擎的正则大小判断错误。

内容的提问来源于stack exchange,提问作者pockey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 01:57:52