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

constructor-properties.mddocs/

Constructor and Properties

RE2 constructor and instance properties for creating and configuring regular expression objects.

Capabilities

RE2 Constructor

Creates a new RE2 regular expression instance with enhanced safety and Buffer support.

/**
 * Creates a new RE2 regular expression instance
 * @param pattern - Regular expression pattern (string, Buffer, RegExp, or RE2)
 * @param flags - Optional flags string or Buffer
 * @returns RE2 instance
 */
function RE2(pattern, flags);
new RE2(pattern, flags);

// Supported pattern types:
new RE2("\\d+");                    // String pattern
new RE2("\\d+", "gi");              // String with flags
new RE2(/\d+/gi);                   // From RegExp
new RE2(existingRE2);               // Copy from RE2
new RE2(Buffer.from("\\d+"));       // Buffer pattern
new RE2("\\d+", Buffer.from("gi")); // Buffer flags

Pattern Types:

  • string - Standard regex pattern string
  • Buffer - UTF-8 encoded pattern
  • RegExp - Copy from existing RegExp (flags can be overridden)
  • RE2 - Copy from existing RE2 instance

Flags:

  • g - Global matching
  • i - Case insensitive
  • m - Multiline mode
  • s - Dot matches newlines (since v1.17.6)
  • u - Unicode mode (always enabled)
  • y - Sticky matching (since v1.7.0)
  • d - Generate indices (since v1.19.0)

Error Handling:

  • Throws TypeError for invalid input types (null, undefined, objects, arrays)
  • Throws SyntaxError for invalid patterns or unsupported features (backreferences, lookahead)

Usage Examples:

const RE2 = require("re2");

// Basic constructor usage
const regex1 = new RE2("\\d+", "g");
const regex2 = RE2("hello", "i"); // Factory function

// Copy from RegExp
const nativeRegex = /test/gi;
const re2Copy = new RE2(nativeRegex);

// Override flags when copying
const re2Modified = new RE2(nativeRegex, "g"); // Only global flag

// Buffer patterns for binary data
const bufferPattern = Buffer.from("\\x[0-9a-f]{2}", "utf8");
const binaryRegex = new RE2(bufferPattern, "gi");

Instance Properties

source

Returns the pattern text excluding flags, escaped for RegExp compatibility.

/**
 * Pattern text excluding flags
 * @type {string}
 * @readonly
 */
regex.source;

Returns '(?:)' for empty patterns. Special characters are escaped for compatibility.

internalSource

Returns the internal RE2 translated source pattern for debugging purposes.

/**
 * Internal RE2 translated source pattern (debugging only)
 * @type {string}  
 * @readonly
 */
regex.internalSource;

Purpose:

  • Exposes the actual pattern used internally by the RE2 engine
  • Different from source property which is escaped for RegExp compatibility
  • Useful for troubleshooting pattern translation differences between JavaScript RegExp and RE2

Usage Example:

const RE2 = require("re2");

// Basic pattern comparison
const regex = new RE2("\\d+");
console.log(regex.source);         // "\\d+" (RegExp-compatible)
console.log(regex.internalSource); // "\\d+" (RE2 internal pattern)

// Complex pattern showing differences
const pathRegex = new RE2("/api/[^/]+/users");
console.log(pathRegex.source);         // "\\/api\\/[^\\/]+\\/users" (escaped)
console.log(pathRegex.internalSource); // "/api/[^/]+/users" (internal)

// Debugging pattern translation issues
const debugRegex = new RE2("test.*end");
if (debugRegex.source !== debugRegex.internalSource) {
  console.log("Pattern was translated:", debugRegex.internalSource);
}

flags

Returns the flags as an alphabetically sorted string.

/**
 * Flag string in alphabetical order
 * @type {string}
 * @readonly
 */
regex.flags;

Example: 'gimsuy' for all flags enabled.

lastIndex

Gets or sets the index for the next match in global regexes.

/**
 * Index for next match (global regexes only)
 * @type {number}
 */
regex.lastIndex;

Reset to 0 after failed matches. Only relevant for global (g) flag regexes.

Flag Properties

global

Whether the global (g) flag is set.

/**
 * Whether global flag is set
 * @type {boolean}
 * @readonly
 */
regex.global;

ignoreCase

Whether the case insensitive (i) flag is set.

/**
 * Whether case insensitive flag is set
 * @type {boolean}
 * @readonly
 */
regex.ignoreCase;

multiline

Whether the multiline (m) flag is set.

/**
 * Whether multiline flag is set
 * @type {boolean}
 * @readonly
 */
regex.multiline;

dotAll

Whether the dot-all (s) flag is set (since v1.17.6).

/**
 * Whether dot-all flag is set
 * @type {boolean}
 * @readonly
 */
regex.dotAll;

Makes . match newline characters.

Usage Example:

const RE2 = require("re2");

// Without dotAll flag - . doesn't match newlines
const normal = new RE2("start.*end");
console.log(normal.test("start\nend")); // false

// With dotAll flag - . matches newlines
const dotAll = new RE2("start.*end", "s");
console.log(dotAll.dotAll);             // true
console.log(dotAll.test("start\nend")); // true

sticky

Whether the sticky (y) flag is set (since v1.7.0).

/**
 * Whether sticky flag is set
 * @type {boolean}
 * @readonly
 */
regex.sticky;

Matches only at lastIndex position.

Usage Example:

const RE2 = require("re2");

// Sticky regex matches only at exact position
const sticky = new RE2("\\d+", "y");
const text = "abc123def456";

console.log(sticky.sticky);    // true
console.log(sticky.lastIndex); // 0

// First match fails because position 0 is not a digit
console.log(sticky.exec(text)); // null
console.log(sticky.lastIndex);  // 0

// Set position to start of first number
sticky.lastIndex = 3;
const match1 = sticky.exec(text);
console.log(match1[0]);         // "123"
console.log(sticky.lastIndex);  // 6

// Next match fails because position 6 is not a digit
console.log(sticky.exec(text)); // null

unicode

Always returns true - RE2 always operates in Unicode mode.

/**
 * Always true - RE2 always operates in Unicode mode
 * @type {boolean}
 * @readonly
 */
regex.unicode;

hasIndices

Whether the indices (d) flag is set (since v1.19.0).

/**
 * Whether indices flag is set
 * @type {boolean}
 * @readonly
 */
regex.hasIndices;

Generates indices for substring matches, providing start/end positions for each capture group.

Usage Example:

const RE2 = require("re2");

// Create regex with indices flag
const regex = new RE2("(\\w+)@(\\w+\\.\\w+)", "d");
console.log(regex.hasIndices); // true

// Execute with indices enabled
const result = regex.exec("contact user@example.com today");
console.log(result[0]);        // "user@example.com" (full match)
console.log(result[1]);        // "user" (first group)
console.log(result[2]);        // "example.com" (second group)

// Access indices array (when hasIndices is true)
if (result.indices) {
  console.log(result.indices[0]); // [8, 23] (full match position)
  console.log(result.indices[1]); // [8, 12] (first group position)
  console.log(result.indices[2]); // [13, 23] (second group position)
}

// Named groups with indices
const namedRegex = new RE2("(?<user>\\w+)@(?<domain>\\w+\\.\\w+)", "d");
const namedResult = namedRegex.exec("user@example.com");
if (namedResult.indices && namedResult.indices.groups) {
  console.log(namedResult.indices.groups.user);   // [0, 4]
  console.log(namedResult.indices.groups.domain); // [5, 15]
}

Static Properties

RE2.unicodeWarningLevel

Controls warning behavior when Unicode flag is automatically added.

/**
 * Controls Unicode flag warning behavior
 * @type {'nothing' | 'warnOnce' | 'warn' | 'throw'}
 */
RE2.unicodeWarningLevel;

Values:

  • 'nothing' - Silent operation (default)
  • 'warnOnce' - Warn once, then silent
  • 'warn' - Warn every time
  • 'throw' - Throw SyntaxError

Usage Example:

const RE2 = require("re2");

// Set warning level
RE2.unicodeWarningLevel = 'warn';

// This will now warn about automatic Unicode flag
const regex = new RE2("test"); // Warns about adding 'u' flag

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