Bindings for RE2: fast, safe alternative to backtracking regular expression engines.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Essential regex methods for pattern matching and testing.
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:
lastIndex for global regexesnull if no match foundUsage 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"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:
true if match found, false otherwiseexec() when only testing for matcheslastIndex for global regexesUsage 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)); // trueReturns a string representation of the regex in standard format.
/**
* String representation of the regex
* @returns String in /pattern/flags format
*/
regex.toString(): string;Behavior:
u (Unicode) flag since RE2 always operates in Unicode mode/pattern/flagsUsage 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)RE2 constructor validates patterns and throws errors for unsupported features.
Supported Features:
[a-z], [^0-9]*, +, ?, {n}, {n,}, {n,m}^, $, \b, \B(), (?:), (?<name>)|\p{L}, \P{N}, etc.Unsupported Features (throw SyntaxError):
\1, \2, etc.(?=...), (?!...)(?<=...), (?<!...)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