CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-re2

Bindings for RE2: fast, safe alternative to backtracking regular expression engines.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

RE2

RE2 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.

Package Information

  • Package Name: re2
  • Package Type: npm
  • Language: JavaScript (with TypeScript support)
  • Installation: npm install re2

Core Imports

const RE2 = require("re2");

For TypeScript/ES modules:

import RE2 from "re2";
// or
import * as RE2 from "re2";

Basic Usage

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 hang

Architecture

RE2 is built around these key components:

  • Constructor Function: Creates RE2 instances that extend RegExp functionality
  • Unicode Mode: Always operates in Unicode mode for consistent text processing
  • Native Binding: Uses Google's C++ RE2 engine for linear-time execution guarantees
  • Buffer Support: Direct processing of Node.js Buffers with UTF-8 encoding
  • Symbol Methods: Full compatibility with ES6+ string method integration
  • Type Safety: Complete TypeScript definitions for all APIs

Capabilities

Constructor and Properties

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 index

Constructor and Properties

Core Methods

Essential 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 representation

Core Methods

String Methods

String 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 matches

String Methods

Buffer Support

Direct 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

Buffer Support

Types

// 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
}

Types

docs

buffer-support.md

constructor-properties.md

core-methods.md

index.md

string-methods.md

types.md

tile.json