🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

@stll/regex-set

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stll/regex-set

Multi-pattern regex for Node.js/Bun. 2-9x faster than JS RegExp, backtracking-immune, Unicode word boundaries. Rust DFA engine via NAPI-RS.

Source
npmnpm
Version
0.1.2
Version published
Weekly downloads
326
30.4%
Maintainers
1
Weekly downloads
 
Created
Source

Stella

@stll/regex-set

NAPI-RS bindings to Rust's regex-automata crate for Node.js and Bun.

Multi-pattern regex matching in a single pass. Guaranteed O(m * n) — no catastrophic backtracking. Built on the same regex engine that powers ripgrep.

Install

npm install @stll/regex-set
# or
bun add @stll/regex-set

Prebuilt binaries are available for:

PlatformArchitecture
macOSx64, arm64
Linux (glibc)x64, arm64
WASMbrowser

Usage

import { RegexSet } from "@stll/regex-set";

const rs = new RegexSet([
  "\\d{2}\\.\\d{2}\\.\\d{4}", // dates
  "\\+?\\d{9,12}", // phones
  "[A-Z]{2}\\d{6}", // IDs
  "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+", // emails
]);

rs.findIter("Born 15.03.1990, ID CZ123456");
// [
//   { pattern: 0, start: 5, end: 15,
//     text: "15.03.1990" },
//   { pattern: 2, start: 20, end: 28,
//     text: "CZ123456" },
// ]

// Quick check
rs.isMatch("call +420123456789"); // true

// Which patterns matched (not where)
rs.whichMatch("call +420123456789"); // [1]

// Replace all matches
rs.replaceAll("Born 15.03.1990, phone +420123456789", [
  "[DATE]",
  "[PHONE]",
  "[ID]",
  "[EMAIL]",
]);
// "Born [DATE], phone [PHONE]"

Named patterns

const rs = new RegexSet([
  { pattern: /\d{2}\.\d{2}\.\d{4}/, name: "date" },
  { pattern: /\+?\d{9,12}/, name: "phone" },
  "[A-Z]{2}\\d{6}", // unnamed
]);

rs.findIter("Born 15.03.1990, ID CZ123456");
// [
//   { pattern: 0, ..., name: "date" },
//   { pattern: 2, ..., text: "CZ123456" },
//   ← no `name` property on unnamed patterns
// ]

Options

const rs = new RegexSet(patterns, {
  // Only match whole words (default: false)
  wholeWords: true,

  // Unicode word boundaries (default: true)
  // Treats accented letters, CJK, etc. as word
  // characters. Auto UAX#29 for Thai/CJK.
  // Set to false for JS RegExp ASCII parity.
  unicodeBoundaries: true,
});

Unicode word boundaries

By default, \b uses Unicode semantics — correct for all scripts. Set unicodeBoundaries: false for JS RegExp ASCII parity:

// Default (Unicode \b): "čáp" is one word (CORRECT)
new RegexSet(["\\bp\\b"]).findIter("čáp");
// → [] (no match — p is inside a word)

// ASCII \b: "p" matches as standalone (WRONG)
new RegexSet(["\\bp\\b"], {
  unicodeBoundaries: false,
}).findIter("čáp");
// → [{ text: "p" }]

// Unicode \b: "čáp" is one word (CORRECT)
new RegexSet(["\\bp\\b"], {
  unicodeBoundaries: true,
}).findIter("čáp");
// → [] (no match — p is inside a word)

new RegexSet(["\\bčáp\\b"], {
  unicodeBoundaries: true,
}).findIter("malý čáp letí");
// → [{ text: "čáp" }]

Implementation: edge \b is stripped from patterns and verified inline per match (two char lookups). The DFA never sees \b, so there is zero overhead regardless of mode. Unicode mode is actually slightly faster because the DFA is simpler.

Lookaround

Lookahead and lookbehind are supported:

const rs = new RegexSet([
  "(?<!\\p{L})IČO:\\s*[0-9]{8}", // lookbehind
  "[0-9]{6}/[0-9]{3,4}(?![0-9])", // lookahead
]);

Internally, lookaround is stripped from patterns, the cores are compiled into a single fast DFA, and assertions are verified as inline char checks on each match (~1ns per check). No backtracking engine involved for simple assertions.

When a greedy quantifier (e.g., \s*) causes the DFA to overshoot past a valid match boundary and the lookahead rejects the longer match, the engine falls back to fancy-regex for that specific match to backtrack the quantifier and find the shorter valid match. This fallback is ~5-6x slower per affected match but ensures correctness; patterns without lookaround are unaffected.

Benchmarks

Measured on Apple M3, 24 GB RAM, macOS 25.3.0. Automaton pre-built; times are search-only averaged over multiple runs.

Corpora: mariomka/regex-benchmark, rust-leipzig/regex-performance, Canterbury Large Corpus.

Run locally: bun run bench:download && bun run bench

Large documents (academic corpora)

Scenario@stll/regex-setJS RegExpSpeedup
mariomka 6.2 MB (3 patterns)20 ms112 ms5.5x
Bible 4 MB (5 multi-pattern)17 ms122 ms7.0x
Bible 4 MB (3 + lookaround)47 ms82 ms1.8x
Twain 16 MB (word boundary)17 ms72 ms4.1x
Twain 16 MB (alternation)12 ms44 ms3.7x
Twain 16 MB (suffix match)24 ms142 ms5.8x

Production PII patterns (13 patterns + lookaround)

Size@stll/regex-setJS RegExpSpeedup
8 KB0.09 ms0.12 ms1.3x
16 KB0.15 ms0.23 ms1.5x
32 KB0.28 ms0.44 ms1.6x
64 KB0.63 ms0.98 ms1.6x
128 KB1.17 ms1.79 ms1.5x
256 KB2.30 ms3.47 ms1.5x

Real Czech contracts (20 anonymization patterns)

Size@stll/regex-setJS RegExpSpeedup
0.6 KB5 μs9 μs1.8x
16 KB80 μs265 μs3.3x
27 KB152 μs448 μs2.9x
63 KB467 μs1016 μs2.2x

Backtracking resistance

PatternInput@stll/regex-setJS RegExp
(a+)+b"a" × 30 + "X"0.12 mshangs
.*.*=.*"x" × 30 + "=" + "y" × 300.25 mshangs

All match counts verified against JS RegExp. For pure literal patterns, use @stll/aho-corasick instead (V8 has a SIMD fast path for literals that no regex engine can match).

Alternatives tested
  • node-re2 — Google RE2 via C++, single pattern per call
  • JS RegExp — V8 built-in, per-pattern loop

API

MethodReturnsDescription
new RegexSet(patterns, options?)instanceCompile patterns
.findIter(haystack)Match[]All non-overlapping matches
.isMatch(haystack)booleanAny pattern matches?
.whichMatch(haystack)number[]Which pattern indices matched
.replaceAll(haystack, replacements)stringReplace matches
.patternCountnumberNumber of patterns

Types

type PatternEntry =
  | string
  | RegExp
  | { pattern: string | RegExp; name?: string };

type Options = {
  wholeWords?: boolean;
  unicodeBoundaries?: boolean;
};

type Match = {
  pattern: number; // which regex matched
  start: number; // UTF-16 code unit offset
  end: number; // exclusive
  text: string; // matched substring
  name?: string; // pattern name (if provided)
};

Same Match type as @stll/aho-corasick: composable results, same UTF-16 offsets compatible with String.prototype.slice().

Regex syntax

Uses Rust regex syntax. Similar to PCRE but:

  • No backreferences (by design: enables O(n))
  • Lookahead/lookbehind supported (via inline char checks, no backtracking)
  • Unicode support by default (\d matches Unicode digits, \w matches Unicode word chars)

Full syntax: docs.rs/regex

Limitations

  • No backreferences. By design: enables the O(n) guarantee. Use JS RegExp for patterns that need backreferences.
  • Single literal patterns are slower than JS. V8 uses SIMD memchr for single literals. Use @stll/aho-corasick for literal string matching.

Acknowledgements

Development

# Install dependencies
bun install

# Build native module (requires Rust toolchain)
bun run build

# Run tests (49 unit + 18 property)
bun test
bun run test:props

# Download benchmark corpora
bun run bench:download

# Run benchmarks
bun run bench

# Lint & format
bun run lint
bun run format

License

MIT

Keywords

multi-pattern

FAQs

Package last updated on 30 Mar 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts