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

如何将检测URL是否以数组项结尾的JS函数改为检测包含数组项

修改URL检测逻辑:从"结尾匹配"到"子串包含"

Hey, I get what you need here—we just need to swap out the matching method in your function to check if the URL contains any of the excluded paths, rather than only matching when the URL ends with them.

Here's the updated function:

var hasUrlCacheExcludeMatch = function(url) {
  var cacheExcludeUrls = [ '/u/register', '/i/new', '/login' ];
  // 把 endsWith 换成 includes,检查URL是否包含指定路径子串
  return cacheExcludeUrls.some(path => url.includes(path));
}

关键改动说明:

  • 原函数用的是url.endsWith(path),只在URL正好以某个排除路径结尾时返回true
  • 修改为url.includes(path)后,只要URL任意位置包含某个排除路径就会返回true,完全符合你的需求

示例验证:

  • 当URL是 https://your-site.com/u/register?source=email:原函数返回false(URL结尾是?source=email而非/u/register),修改后的函数返回true(包含目标子串/u/register)
  • 当URL是 https://your-site.com/login/2fa:原函数返回false(URL结尾是/2fa),修改后的函数返回true(包含目标子串/login)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 09:10:38