Bindings for RE2: fast, safe alternative to backtracking regular expression engines.
npx @tessl/cli install tessl/npm-re2@1.22.0RE2 is a Node.js binding for Google's RE2 regular expression engine, providing a fast and safe alternative to JavaScript's built-in RegExp. It offers protection against Regular Expression Denial of Service (ReDoS) attacks while maintaining near-complete API compatibility with native RegExp.
npm install re2const RE2 = require("re2");For TypeScript/ES modules:
import RE2 from "re2";
// or
import * as RE2 from "re2";const RE2 = require("re2");
// Create a new RE2 instance (drop-in RegExp replacement)
const regex = new RE2("\\d+", "g");
console.log(regex.test("123")); // true
// Use with string methods
const text = "Phone: 555-1234, Fax: 555-5678";
const matches = text.match(regex);
console.log(matches); // ["555", "1234", "555", "5678"]
// Safe from ReDoS attacks
const safeRegex = new RE2("(a+)+b");
const result = safeRegex.test("a".repeat(1000)); // Won't hangRE2 is built around these key components:
Core RE2 constructor and instance properties for creating and configuring regular expression objects.
// Constructor
function RE2(pattern, flags);
new RE2(pattern, flags);
// Instance properties
regex.source; // Pattern string
regex.flags; // Flag string
regex.global; // Boolean flag properties
regex.ignoreCase; // ...
regex.lastIndex; // Current match indexEssential regex methods for pattern matching and testing.
regex.exec(str); // Execute regex, return match details
regex.test(str); // Test if pattern matches
regex.toString(); // String representationString processing methods compatible with JavaScript's built-in string operations.
regex.match(str); // Find matches
regex.search(str); // Find match index
regex.replace(str, replacement); // Replace matches
regex.split(str, limit); // Split by matchesDirect Buffer processing for efficient text operations without string conversion overhead.
regex.exec(buffer); // Execute on Buffer
regex.test(buffer); // Test Buffer
regex.match(buffer); // Match in Buffer
RE2.getUtf8Length(str); // UTF-8 length calculation
RE2.getUtf16Length(buffer); // UTF-16 length calculation// Constructor signatures
interface RE2Constructor {
new(pattern: string | Buffer | RegExp | RE2, flags?: string | Buffer): RE2;
(pattern: string | Buffer | RegExp | RE2, flags?: string | Buffer): RE2;
// Static properties and methods
unicodeWarningLevel: 'nothing' | 'warnOnce' | 'warn' | 'throw';
getUtf8Length(value: string): number;
getUtf16Length(value: Buffer): number;
}
// Instance interface
interface RE2 extends RegExp {
// Enhanced methods with Buffer support
exec(str: string | Buffer): RegExpExecArray | RE2BufferExecArray | null;
test(str: string | Buffer): boolean;
match(str: string | Buffer): RegExpMatchArray | RE2BufferMatchArray | null;
// ... additional methods
}