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

Overview
Eval results
Files

core-methods.mddocs/

Core Methods

Essential regex methods for pattern matching and testing.

Capabilities

exec Method

Executes the regex against a string or Buffer and returns detailed match information.

/**
 * Execute regex against input and return match details
 * @param str - String or Buffer to search
 * @returns Match array with details or null if no match
 */
regex.exec(str: string): RegExpExecArray | null;
regex.exec(buffer: Buffer): RE2BufferExecArray | null;

// Return types
interface RegExpExecArray extends Array<string> {
  index: number;           // Match start position (characters)
  input: string;          // Original input string
  groups?: {              // Named capture groups
    [key: string]: string;
  };
}

interface RE2BufferExecArray extends Array<Buffer> {
  index: number;          // Match start position (bytes)
  input: Buffer;         // Original input Buffer
  groups?: {             // Named capture groups
    [key: string]: Buffer;
  };
}

Behavior:

  • Updates lastIndex for global regexes
  • Returns null if no match found
  • For strings: positions in characters, results as strings
  • For Buffers: positions in bytes, results as Buffers

Usage Examples:

const RE2 = require("re2");

// String execution
const regex = new RE2("(\\d{3})-(\\d{4})", "g");
const text = "Call 555-1234 or 555-5678";

let match;
while ((match = regex.exec(text)) !== null) {
  console.log(match[0]);    // Full match: "555-1234"
  console.log(match[1]);    // First group: "555"
  console.log(match[2]);    // Second group: "1234"
  console.log(match.index); // Position: 5
}

// Buffer execution
const bufferRegex = new RE2("hello");
const buffer = Buffer.from("hello world", "utf8");
const bufferMatch = bufferRegex.exec(buffer);
console.log(bufferMatch[0]); // Buffer containing "hello"
console.log(bufferMatch.index); // 0 (byte position)

// Named groups
const namedRegex = new RE2("(?<area>\\d{3})-(?<number>\\d{4})");
const namedMatch = namedRegex.exec("555-1234");
console.log(namedMatch.groups.area);   // "555"
console.log(namedMatch.groups.number); // "1234"

test Method

Tests whether the regex matches in the given input without returning match details.

/**
 * Test if regex matches in input
 * @param str - String or Buffer to test
 * @returns Boolean indicating match success
 */
regex.test(str: string | Buffer): boolean;

Behavior:

  • Returns true if match found, false otherwise
  • More efficient than exec() when only testing for matches
  • Updates lastIndex for global regexes
  • Works with both strings and Buffers

Usage Examples:

const RE2 = require("re2");

// Basic testing
const emailRegex = new RE2("\\w+@\\w+\\.\\w+");
console.log(emailRegex.test("user@example.com")); // true
console.log(emailRegex.test("invalid-email"));    // false

// Global regex testing
const globalRegex = new RE2("\\d+", "g");
const numbers = "1 2 3 4";
console.log(globalRegex.test(numbers)); // true (finds "1")
console.log(globalRegex.test(numbers)); // true (finds "2") 
console.log(globalRegex.test(numbers)); // true (finds "3")
console.log(globalRegex.test(numbers)); // true (finds "4")
console.log(globalRegex.test(numbers)); // false (no more matches)

// Buffer testing
const bufferRegex = new RE2("test");
const buffer = Buffer.from("test data", "utf8");
console.log(bufferRegex.test(buffer)); // true

toString Method

Returns a string representation of the regex in standard format.

/**
 * String representation of the regex
 * @returns String in /pattern/flags format
 */
regex.toString(): string;

Behavior:

  • Always includes the u (Unicode) flag since RE2 always operates in Unicode mode
  • Format: /pattern/flags
  • Escapes special characters appropriately

Usage Examples:

const RE2 = require("re2");

const regex1 = new RE2("\\d+", "gi");
console.log(regex1.toString()); // "/\\d+/giu"

const regex2 = new RE2("hello");
console.log(regex2.toString()); // "/hello/u"

const regex3 = new RE2("test", "gmsyid");
console.log(regex3.toString()); // "/test/dgimsyu" (flags alphabetically sorted)

Pattern Validation

RE2 constructor validates patterns and throws errors for unsupported features.

Supported Features:

  • Standard regex syntax
  • Character classes: [a-z], [^0-9]
  • Quantifiers: *, +, ?, {n}, {n,}, {n,m}
  • Anchors: ^, $, \b, \B
  • Groups: (), (?:), (?<name>)
  • Alternation: |
  • Unicode properties: \p{L}, \P{N}, etc.

Unsupported Features (throw SyntaxError):

  • Backreferences: \1, \2, etc.
  • Lookahead assertions: (?=...), (?!...)
  • Lookbehind assertions: (?<=...), (?<!...)

Usage Examples:

const RE2 = require("re2");

// Valid patterns
const valid1 = new RE2("\\d+");           // ✓ Basic pattern
const valid2 = new RE2("(?<name>\\w+)");  // ✓ Named groups
const valid3 = new RE2("\\p{L}+");        // ✓ Unicode properties

// Invalid patterns (throw SyntaxError)
try {
  const invalid1 = new RE2("(\\w+)\\1");     // ✗ Backreference
} catch (e) {
  console.log(e.message); // SyntaxError about backreferences
}

try {
  const invalid2 = new RE2("(?=test)");      // ✗ Lookahead
} catch (e) {
  console.log(e.message); // SyntaxError about lookahead
}

Install with Tessl CLI

npx tessl i tessl/npm-re2

docs

buffer-support.md

constructor-properties.md

core-methods.md

index.md

string-methods.md

types.md

tile.json