or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

buffer-support.mdconstructor-properties.mdcore-methods.mdindex.mdstring-methods.mdtypes.md

constructor-properties.mddocs/

0

# Constructor and Properties

1

2

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

3

4

## Capabilities

5

6

### RE2 Constructor

7

8

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

9

10

```javascript { .api }

11

/**

12

* Creates a new RE2 regular expression instance

13

* @param pattern - Regular expression pattern (string, Buffer, RegExp, or RE2)

14

* @param flags - Optional flags string or Buffer

15

* @returns RE2 instance

16

*/

17

function RE2(pattern, flags);

18

new RE2(pattern, flags);

19

20

// Supported pattern types:

21

new RE2("\\d+"); // String pattern

22

new RE2("\\d+", "gi"); // String with flags

23

new RE2(/\d+/gi); // From RegExp

24

new RE2(existingRE2); // Copy from RE2

25

new RE2(Buffer.from("\\d+")); // Buffer pattern

26

new RE2("\\d+", Buffer.from("gi")); // Buffer flags

27

```

28

29

**Pattern Types:**

30

- `string` - Standard regex pattern string

31

- `Buffer` - UTF-8 encoded pattern

32

- `RegExp` - Copy from existing RegExp (flags can be overridden)

33

- `RE2` - Copy from existing RE2 instance

34

35

**Flags:**

36

- `g` - Global matching

37

- `i` - Case insensitive

38

- `m` - Multiline mode

39

- `s` - Dot matches newlines (since v1.17.6)

40

- `u` - Unicode mode (always enabled)

41

- `y` - Sticky matching (since v1.7.0)

42

- `d` - Generate indices (since v1.19.0)

43

44

**Error Handling:**

45

- Throws `TypeError` for invalid input types (null, undefined, objects, arrays)

46

- Throws `SyntaxError` for invalid patterns or unsupported features (backreferences, lookahead)

47

48

**Usage Examples:**

49

50

```javascript

51

const RE2 = require("re2");

52

53

// Basic constructor usage

54

const regex1 = new RE2("\\d+", "g");

55

const regex2 = RE2("hello", "i"); // Factory function

56

57

// Copy from RegExp

58

const nativeRegex = /test/gi;

59

const re2Copy = new RE2(nativeRegex);

60

61

// Override flags when copying

62

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

63

64

// Buffer patterns for binary data

65

const bufferPattern = Buffer.from("\\x[0-9a-f]{2}", "utf8");

66

const binaryRegex = new RE2(bufferPattern, "gi");

67

```

68

69

### Instance Properties

70

71

#### source

72

73

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

74

75

```javascript { .api }

76

/**

77

* Pattern text excluding flags

78

* @type {string}

79

* @readonly

80

*/

81

regex.source;

82

```

83

84

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

85

86

#### internalSource

87

88

Returns the internal RE2 translated source pattern for debugging purposes.

89

90

```javascript { .api }

91

/**

92

* Internal RE2 translated source pattern (debugging only)

93

* @type {string}

94

* @readonly

95

*/

96

regex.internalSource;

97

```

98

99

**Purpose:**

100

- Exposes the actual pattern used internally by the RE2 engine

101

- Different from `source` property which is escaped for RegExp compatibility

102

- Useful for troubleshooting pattern translation differences between JavaScript RegExp and RE2

103

104

**Usage Example:**

105

106

```javascript

107

const RE2 = require("re2");

108

109

// Basic pattern comparison

110

const regex = new RE2("\\d+");

111

console.log(regex.source); // "\\d+" (RegExp-compatible)

112

console.log(regex.internalSource); // "\\d+" (RE2 internal pattern)

113

114

// Complex pattern showing differences

115

const pathRegex = new RE2("/api/[^/]+/users");

116

console.log(pathRegex.source); // "\\/api\\/[^\\/]+\\/users" (escaped)

117

console.log(pathRegex.internalSource); // "/api/[^/]+/users" (internal)

118

119

// Debugging pattern translation issues

120

const debugRegex = new RE2("test.*end");

121

if (debugRegex.source !== debugRegex.internalSource) {

122

console.log("Pattern was translated:", debugRegex.internalSource);

123

}

124

```

125

126

#### flags

127

128

Returns the flags as an alphabetically sorted string.

129

130

```javascript { .api }

131

/**

132

* Flag string in alphabetical order

133

* @type {string}

134

* @readonly

135

*/

136

regex.flags;

137

```

138

139

Example: `'gimsuy'` for all flags enabled.

140

141

#### lastIndex

142

143

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

144

145

```javascript { .api }

146

/**

147

* Index for next match (global regexes only)

148

* @type {number}

149

*/

150

regex.lastIndex;

151

```

152

153

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

154

155

### Flag Properties

156

157

#### global

158

159

Whether the global (`g`) flag is set.

160

161

```javascript { .api }

162

/**

163

* Whether global flag is set

164

* @type {boolean}

165

* @readonly

166

*/

167

regex.global;

168

```

169

170

#### ignoreCase

171

172

Whether the case insensitive (`i`) flag is set.

173

174

```javascript { .api }

175

/**

176

* Whether case insensitive flag is set

177

* @type {boolean}

178

* @readonly

179

*/

180

regex.ignoreCase;

181

```

182

183

#### multiline

184

185

Whether the multiline (`m`) flag is set.

186

187

```javascript { .api }

188

/**

189

* Whether multiline flag is set

190

* @type {boolean}

191

* @readonly

192

*/

193

regex.multiline;

194

```

195

196

#### dotAll

197

198

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

199

200

```javascript { .api }

201

/**

202

* Whether dot-all flag is set

203

* @type {boolean}

204

* @readonly

205

*/

206

regex.dotAll;

207

```

208

209

Makes `.` match newline characters.

210

211

**Usage Example:**

212

213

```javascript

214

const RE2 = require("re2");

215

216

// Without dotAll flag - . doesn't match newlines

217

const normal = new RE2("start.*end");

218

console.log(normal.test("start\nend")); // false

219

220

// With dotAll flag - . matches newlines

221

const dotAll = new RE2("start.*end", "s");

222

console.log(dotAll.dotAll); // true

223

console.log(dotAll.test("start\nend")); // true

224

```

225

226

#### sticky

227

228

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

229

230

```javascript { .api }

231

/**

232

* Whether sticky flag is set

233

* @type {boolean}

234

* @readonly

235

*/

236

regex.sticky;

237

```

238

239

Matches only at `lastIndex` position.

240

241

**Usage Example:**

242

243

```javascript

244

const RE2 = require("re2");

245

246

// Sticky regex matches only at exact position

247

const sticky = new RE2("\\d+", "y");

248

const text = "abc123def456";

249

250

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

251

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

252

253

// First match fails because position 0 is not a digit

254

console.log(sticky.exec(text)); // null

255

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

256

257

// Set position to start of first number

258

sticky.lastIndex = 3;

259

const match1 = sticky.exec(text);

260

console.log(match1[0]); // "123"

261

console.log(sticky.lastIndex); // 6

262

263

// Next match fails because position 6 is not a digit

264

console.log(sticky.exec(text)); // null

265

```

266

267

#### unicode

268

269

Always returns `true` - RE2 always operates in Unicode mode.

270

271

```javascript { .api }

272

/**

273

* Always true - RE2 always operates in Unicode mode

274

* @type {boolean}

275

* @readonly

276

*/

277

regex.unicode;

278

```

279

280

#### hasIndices

281

282

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

283

284

```javascript { .api }

285

/**

286

* Whether indices flag is set

287

* @type {boolean}

288

* @readonly

289

*/

290

regex.hasIndices;

291

```

292

293

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

294

295

**Usage Example:**

296

297

```javascript

298

const RE2 = require("re2");

299

300

// Create regex with indices flag

301

const regex = new RE2("(\\w+)@(\\w+\\.\\w+)", "d");

302

console.log(regex.hasIndices); // true

303

304

// Execute with indices enabled

305

const result = regex.exec("contact user@example.com today");

306

console.log(result[0]); // "user@example.com" (full match)

307

console.log(result[1]); // "user" (first group)

308

console.log(result[2]); // "example.com" (second group)

309

310

// Access indices array (when hasIndices is true)

311

if (result.indices) {

312

console.log(result.indices[0]); // [8, 23] (full match position)

313

console.log(result.indices[1]); // [8, 12] (first group position)

314

console.log(result.indices[2]); // [13, 23] (second group position)

315

}

316

317

// Named groups with indices

318

const namedRegex = new RE2("(?<user>\\w+)@(?<domain>\\w+\\.\\w+)", "d");

319

const namedResult = namedRegex.exec("user@example.com");

320

if (namedResult.indices && namedResult.indices.groups) {

321

console.log(namedResult.indices.groups.user); // [0, 4]

322

console.log(namedResult.indices.groups.domain); // [5, 15]

323

}

324

```

325

326

### Static Properties

327

328

#### RE2.unicodeWarningLevel

329

330

Controls warning behavior when Unicode flag is automatically added.

331

332

```javascript { .api }

333

/**

334

* Controls Unicode flag warning behavior

335

* @type {'nothing' | 'warnOnce' | 'warn' | 'throw'}

336

*/

337

RE2.unicodeWarningLevel;

338

```

339

340

**Values:**

341

- `'nothing'` - Silent operation (default)

342

- `'warnOnce'` - Warn once, then silent

343

- `'warn'` - Warn every time

344

- `'throw'` - Throw SyntaxError

345

346

**Usage Example:**

347

348

```javascript

349

const RE2 = require("re2");

350

351

// Set warning level

352

RE2.unicodeWarningLevel = 'warn';

353

354

// This will now warn about automatic Unicode flag

355

const regex = new RE2("test"); // Warns about adding 'u' flag

356

```