@ericcornelissen/lregexp

中文文档
1.0.13发布于 2026/06/20MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 1.1万

Transparent linear-time (non-backtracking) regular expressions for libraries

原始使用文档
文档目录9 节
  1. Usage
  2. Runtimes
  3. Background
  4. Why
  5. Caveats
  6. Example
  7. Rewriting
  8. Testing
  9. License

lRegExp

Transparent linear-time (non-backtracking) regular expressions for libraries.

Usage

This package exports a drop-in replacement for the built-in RegExp (caveats apply).

  1. Install

    npm install @ericcornelissen/lregexp
    
  2. Import

    import RegExp from "@ericcornelissen/lregexp";
    
  3. Use

    new RegExp("[linear]{6}");
    

Runtimes

This package is compatible with Node.js, Deno, Bun, and major browsers. It only takes effect in V8-based runtimes (Node.js, Deno, Chromium-based browsers) when the experimental linear-time engine is enabled. Otherwise it falls back to the default RegExp constructor.

To enable the linear-time regular expression engine:

  • Node.js: use node --enable-experimental-regexp-engine entrypoint.js
  • Deno: use DENO_V8_FLAGS="--enable-experimental-regexp-engine" deno entrypoint.ts

Background

Why

Backtracking regular expressions can take exponential time to evaluate, leading to the dreaded ReDoS vulnerability. Linear-time regular expressions avoid this by not backtracking.

In V8-based runtimes, linear-time regular expressions can be created using the l flag provided the --enable-experimental-regexp-engine CLI option is used. This makes it difficult for library authors to tap into. Using this package, a library can use the RegExp constructor as usual and benefit from the linear time regular expression engine when its users enable it. If they don't, it gracefully falls back to the default constructor.

Caveats

Not all valid JavaScript regular expressions are supported when using the --enable-experimental-regexp-engine CLI option. This package won't tell you if your regular expressions are incompatible, unless you run it with that CLI option. If a regular expression is incompatible, the constructor will throw a SyntaxError.

To support users of this package in writing compatible regular expressions we're interested in:

  • an ESLint plugin to lint regular expressions and raise warnings for the use of regex features not supported by the non-backtracking engine; (#4)
  • a tool (CLI/web) to check a given regular expression for compatibility with the non-backtracking engine. (#21)

Example

A classic example of a ReDoS-vulnerable regular expression is (a*)*b. Using this with vanilla Node.js on a pathological input string takes quite some time, test it for yourself with (add an a and the runtime doubles):

node -e '/(a*)*b/.test("aaaaaaaaaaaaaaaaaaaaaaaaac")'

When the non-backtracking regular expression engine is enabled, the expression evaluates instantly:

node -e '/(a*)*b/l.test("aaaaaaaaaaaaaaaaaaaaaaaaac")' --enable-experimental-regexp-engine

Rewriting

The following are some suggestions for rewriting regular expressions from JavaScript's standard engine to the linear-time engine. These suggestions are not universally applicable and careful testing is still required.

  • Positive lookahead ((?=...)) when replacing

    Capture the lookahead and restore it (using $<n>) when replacing. For example: replace(/(a(?=b)/, "A") to replace(/a(b)/, "A$1").

  • Negative lookahead ((?!...)) when replacing

    Capture the lookahead, invert the match, add an end-of-string anchor ($), and restore it (using $<n>) when replacing. For example: replace(/(a(?!b)/, "A") to replace(/a([^b]|$)/, "A$1").

  • i flag when testing

    Write the regular expression to match lowercase strings and convert strings to lowercase (using .toLowerCase()) before matching them.

Testing

We recommend using differential testing using fast-check when rewriting regular expressions. You can use the following test example as a template:

// Run as: node --test --enable-experimental-regexp-engine rewrite_test.js

import { test } from "node:test";

import lRegExp from "@ericcornelissen/lregexp";
import * as fc from "fast-check";

test("rewritten regular expression", () => {
  const original = /old regular expression/;
  const rewritten = new lRegExp(/new regular expression/);

  fc.assert(
    fc.property(
      fc.oneof(
        // any old string, unlikely to match
        fc.string(), 

        // strings that should match (limited support)
        fc.stringMatching(original), 
      ),
      (s) => original.test(s) === rewritten.test(s)
    ),
  );
});

test("--enable-experimental-regexp-engine", () => {
  /ensures that the linear engine is enabled/l;
});

License

The source code is licensed under the MIT license, see LICENSE for the full license text. The documentation text is licensed under CC0-1.0; code snippets under the MIT-0 license.

版本与发布记录

@ericcornelissen/lregexp 共发布 14 个版本,首次发布于 2025/08/19;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.13latest2026/06/204 个月前
1.0.122026/06/184 个月前
1.0.112026/06/144 个月前
1.0.102026/06/045 个月前
1.0.92026/05/066 个月前
1.0.82026/03/048 个月前

关于 @ericcornelissen/lregexp 的常见问题

@ericcornelissen/lregexp 是什么?

Transparent linear-time (non-backtracking) regular expressions for libraries

如何安装 @ericcornelissen/lregexp?

在项目目录运行 npm install @ericcornelissen/lregexp,或使用 pnpm add / yarn add / bun add。

@ericcornelissen/lregexp 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ericcornelissen/lregexp 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ericcornelissen/lregexp 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^0.8.6 || >=0.9.1 <27。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ericcornelissen/lregexp 可以商用吗?

@ericcornelissen/lregexp 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ericcornelissen/lregexp 的下载量有多少?

根据 npm 官方下载统计,@ericcornelissen/lregexp 在 2026/10/14 – 2026/11/12 这 30 天内被下载 10,602 次,日均约 353 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ericcornelissen/lregexp 还在维护吗?

最新版本 1.0.13 发布于 2026/06/20(4 个月前);至今共发布 14 个版本,其中过去 12 个月发布了 11 个;首次发布于 2025/08/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。