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

Firefox不支持后向查找正则,求替代正则或解决方案

Solution: Split Strings Without Breaking Slashes After "abc" in Firefox

Hey there! Since Firefox doesn't support negative lookbehind ((?<!...)) — the feature your original regex /(?<!abc)\//g depends on — we’ve got two reliable ways to get this done: a browser-compatible regex rewrite, and a straightforward non-regex approach. Let’s dive in.

1. Firefox-Friendly Rewrite with Regex

Instead of relying on unsupported lookbehind, we can use either a temporary marker trick (super easy to follow) or a positive lookahead-based regex match.

Option A: Temporary Marker Replacement

This method works across all browsers, including Firefox, and is hard to mess up:

const inputStr = "test/abc/foo";
// First, replace "abc/" with a unique temporary marker
const tempStr = inputStr.replace(/abc\//g, "TEMP_ABC_SLASH");
// Split on all remaining slashes
const splitParts = tempStr.split("/");
// Restore the original "abc/" in each part
const finalResult = splitParts.map(part => part.replace(/TEMP_ABC_SLASH/g, "abc/"));

console.log(finalResult); // Output: ["test", "abc/foo"]

The idea is to "protect" the slashes we don’t want to split first, then split normally, and finally put the protected parts back.

Option B: Match Target Segments Directly

If you prefer to skip the replacement step, use a regex with negative lookahead (which Firefox does support) to capture the segments we want:

const inputStr = "test/abc/foo";
const segmentRegex = /(?:(?!abc\/).)+|abc\/.*/g;
const finalResult = inputStr.match(segmentRegex);

console.log(finalResult); // Output: ["test", "abc/foo"]

Here’s what the regex does:

  • (?:(?!abc\/).)+: Matches any sequence of characters that doesn’t lead into "abc/"
  • |: Acts as an "or" operator
  • abc\/.*: Matches "abc/" and everything that comes after it (so we keep this whole chunk intact)

2. Non-Regex Alternative

If regex isn’t your vibe, a simple string traversal works perfectly. We’ll build segments character by character, only splitting on slashes that aren’t preceded by "abc":

function splitExceptAfterABC(inputStr) {
  const segments = [];
  let currentSegment = "";

  for (let i = 0; i < inputStr.length; i++) {
    const char = inputStr[i];
    if (char === "/") {
      // Check if the 3 characters before this slash are "abc"
      if (i >= 3 && inputStr.slice(i - 3, i) === "abc") {
        // Keep this slash in the current segment
        currentSegment += char;
      } else {
        // Split here: add current segment to the list, reset for next
        segments.push(currentSegment);
        currentSegment = "";
      }
    } else {
      currentSegment += char;
    }
  }

  // Don't forget to add the last segment
  if (currentSegment) segments.push(currentSegment);
  return segments;
}

// Test it out
console.log(splitExceptAfterABC("test/abc/foo")); // ["test", "abc/foo"]
console.log(splitExceptAfterABC("a/b/abc/c/d")); // ["a", "b", "abc/c/d"]

This approach is totally regex-free, super readable, and works in every browser without exceptions.

Wrap-Up

  • Go with the temporary marker regex method if you want something quick and compatible.
  • Use the direct regex match if you prefer a one-step regex solution.
  • Pick the non-regex traversal if you want to avoid regex entirely or need maximum readability.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:15:09