0
# RE2
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: re2
7
- **Package Type**: npm
8
- **Language**: JavaScript (with TypeScript support)
9
- **Installation**: `npm install re2`
10
11
## Core Imports
12
13
```javascript
14
const RE2 = require("re2");
15
```
16
17
For TypeScript/ES modules:
18
19
```typescript
20
import RE2 from "re2";
21
// or
22
import * as RE2 from "re2";
23
```
24
25
## Basic Usage
26
27
```javascript
28
const RE2 = require("re2");
29
30
// Create a new RE2 instance (drop-in RegExp replacement)
31
const regex = new RE2("\\d+", "g");
32
console.log(regex.test("123")); // true
33
34
// Use with string methods
35
const text = "Phone: 555-1234, Fax: 555-5678";
36
const matches = text.match(regex);
37
console.log(matches); // ["555", "1234", "555", "5678"]
38
39
// Safe from ReDoS attacks
40
const safeRegex = new RE2("(a+)+b");
41
const result = safeRegex.test("a".repeat(1000)); // Won't hang
42
```
43
44
## Architecture
45
46
RE2 is built around these key components:
47
48
- **Constructor Function**: Creates RE2 instances that extend RegExp functionality
49
- **Unicode Mode**: Always operates in Unicode mode for consistent text processing
50
- **Native Binding**: Uses Google's C++ RE2 engine for linear-time execution guarantees
51
- **Buffer Support**: Direct processing of Node.js Buffers with UTF-8 encoding
52
- **Symbol Methods**: Full compatibility with ES6+ string method integration
53
- **Type Safety**: Complete TypeScript definitions for all APIs
54
55
## Capabilities
56
57
### Constructor and Properties
58
59
Core RE2 constructor and instance properties for creating and configuring regular expression objects.
60
61
```javascript { .api }
62
// Constructor
63
function RE2(pattern, flags);
64
new RE2(pattern, flags);
65
66
// Instance properties
67
regex.source; // Pattern string
68
regex.flags; // Flag string
69
regex.global; // Boolean flag properties
70
regex.ignoreCase; // ...
71
regex.lastIndex; // Current match index
72
```
73
74
[Constructor and Properties](./constructor-properties.md)
75
76
### Core Methods
77
78
Essential regex methods for pattern matching and testing.
79
80
```javascript { .api }
81
regex.exec(str); // Execute regex, return match details
82
regex.test(str); // Test if pattern matches
83
regex.toString(); // String representation
84
```
85
86
[Core Methods](./core-methods.md)
87
88
### String Methods
89
90
String processing methods compatible with JavaScript's built-in string operations.
91
92
```javascript { .api }
93
regex.match(str); // Find matches
94
regex.search(str); // Find match index
95
regex.replace(str, replacement); // Replace matches
96
regex.split(str, limit); // Split by matches
97
```
98
99
[String Methods](./string-methods.md)
100
101
### Buffer Support
102
103
Direct Buffer processing for efficient text operations without string conversion overhead.
104
105
```javascript { .api }
106
regex.exec(buffer); // Execute on Buffer
107
regex.test(buffer); // Test Buffer
108
regex.match(buffer); // Match in Buffer
109
RE2.getUtf8Length(str); // UTF-8 length calculation
110
RE2.getUtf16Length(buffer); // UTF-16 length calculation
111
```
112
113
[Buffer Support](./buffer-support.md)
114
115
## Types
116
117
```javascript { .api }
118
// Constructor signatures
119
interface RE2Constructor {
120
new(pattern: string | Buffer | RegExp | RE2, flags?: string | Buffer): RE2;
121
(pattern: string | Buffer | RegExp | RE2, flags?: string | Buffer): RE2;
122
123
// Static properties and methods
124
unicodeWarningLevel: 'nothing' | 'warnOnce' | 'warn' | 'throw';
125
getUtf8Length(value: string): number;
126
getUtf16Length(value: Buffer): number;
127
}
128
129
// Instance interface
130
interface RE2 extends RegExp {
131
// Enhanced methods with Buffer support
132
exec(str: string | Buffer): RegExpExecArray | RE2BufferExecArray | null;
133
test(str: string | Buffer): boolean;
134
match(str: string | Buffer): RegExpMatchArray | RE2BufferMatchArray | null;
135
// ... additional methods
136
}
137
```
138
139
[Types](./types.md)