{"_id":"@adguard/re2-wasm","_rev":"3-ce9f29afedf3787958e0510a9a85d26d","name":"@adguard/re2-wasm","dist-tags":{"latest":"1.2.1"},"versions":{"1.2.0":{"name":"@adguard/re2-wasm","version":"1.2.0","keywords":["regexp","re2","wasm"],"author":"","license":"Apache-2.0","_id":"@adguard/re2-wasm@1.2.0","maintainers":[{"name":"ameshkov","email":"am@adguard.com"},{"name":"maximtop","email":"maximtop@gmail.com"},{"name":"blakhard","email":"vlad.abdulmianov@gmail.com"}],"homepage":"https://github.com/AdguardTeam/re2-wasm","bugs":{"url":"https://github.com/AdguardTeam/re2-wasm/issues"},"dist":{"shasum":"b8f6d7f60c884c50273814acf1b6af1674646be9","tarball":"https://registry.npmjs.org/@adguard/re2-wasm/-/re2-wasm-1.2.0.tgz","fileCount":11,"integrity":"sha512-yMtEJfThxN9rEYm2Zg3X8jUKaPYq5wByc90JbMspSWJo7MXw664gTvLD7/wijl6y79VSuAs77bXUIgBXutMp7A==","signatures":[{"sig":"MEQCID7I979XxxMlLSLJnFa9LXYaQZe6YlpKqshWQ94yArXxAiACwFIoi2o+suRr3kPgtlaqbXnJnrHpCqedl/TqxXjIfg==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":1126270},"main":"build/src/re2.js","_from":"file:re2-wasm.tgz","types":"./build/src/re2.d.ts","engines":{"node":">=10"},"scripts":{"fix":"gts fix src/*.ts","lint":"gts lint src/*.ts","test":"node ./third_party/node-re2/tests/tests.js","clean":"gts clean","compile":"make -j12 && tsc && cp -r wasm build/ && node scripts/build-info.js","prepare":"npm run compile","pretest":"npm run compile","posttest":"npm run lint","increment":"npm version patch --no-git-tag-version","compile-ts":"tsc && cp -r wasm build/","compile-emcc":"docker run --rm -v $(pwd):/src emscripten/emsdk make -j12"},"_npmUser":{"name":"maximtop","email":"maximtop@gmail.com"},"_resolved":"/opt/atlassian/application-data/bamboo/shared/build-dir/1933315/202145795-202211331/re2-wasm.tgz","_integrity":"sha512-yMtEJfThxN9rEYm2Zg3X8jUKaPYq5wByc90JbMspSWJo7MXw664gTvLD7/wijl6y79VSuAs77bXUIgBXutMp7A==","repository":{"url":"git+https://github.com/AdguardTeam/re2-wasm.git","type":"git"},"_npmVersion":"10.5.0","description":"Google's RE2 library distributed as a WASM module patched by AdGuard.","directories":{},"_nodeVersion":"21.7.3","_hasShrinkwrap":false,"devDependencies":{"gts":"^3.1.0","heya-unit":"^0.3.0","typescript":"~4.1.3","@types/node":"^14.11.2"},"_npmOperationalInternal":{"tmp":"tmp/re2-wasm_1.2.0_1718810826286_0.08436853141250289","host":"s3://npm-registry-packages"}},"1.2.1":{"name":"@adguard/re2-wasm","version":"1.2.1","keywords":["regexp","re2","wasm"],"author":"","license":"Apache-2.0","_id":"@adguard/re2-wasm@1.2.1","maintainers":[{"name":"ameshkov","email":"am@adguard.com"},{"name":"maximtop","email":"maximtop@gmail.com"},{"name":"blakhard","email":"vlad.abdulmianov@gmail.com"}],"homepage":"https://github.com/AdguardTeam/re2-wasm","bugs":{"url":"https://github.com/AdguardTeam/re2-wasm/issues"},"dist":{"shasum":"84a980b8a7f45437dad5f2b6a0b4a3bb350d1b58","tarball":"https://registry.npmjs.org/@adguard/re2-wasm/-/re2-wasm-1.2.1.tgz","fileCount":11,"integrity":"sha512-lxl4BUNI9xMNKLdnguonacLMuPhv/JNiIAVGRQKI0WyPw+6W0ceDJhWzrR4Gy7p2zp+2hAH5JNTD/1YjCCR82g==","signatures":[{"sig":"MEUCIQCtbBluFh+nEh8CbgP1YMlrglAhmUHdbrhXSpUVzSxJRQIgVdm+wp8VNERqOcbJH+0TIBmI0oO2Du2kh3WeprkSy0g=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":1124650},"main":"build/src/re2.js","_from":"file:re2-wasm.tgz","types":"./build/src/re2.d.ts","engines":{"node":">=10"},"scripts":{"fix":"gts fix src/*.ts","lint":"npm run lint:ts && npm run lint:md","test":"node ./third_party/node-re2/tests/tests.js","clean":"gts clean","compile":"make -j12 && tsc && cp -r wasm build/","lint:md":"markdownlint .","lint:ts":"gts lint src/*.ts","prepare":"npm run compile","pretest":"npm run compile","posttest":"npm run lint","compile-ts":"tsc && cp -r wasm build/","compile-emcc":"docker run --rm -v $(pwd):/src emscripten/emsdk make -j12"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:40723252-e33b-4b8f-96cd-ade0282533e6"}},"_resolved":"/home/runner/work/ext-re2-wasm/ext-re2-wasm/re2-wasm.tgz","_integrity":"sha512-lxl4BUNI9xMNKLdnguonacLMuPhv/JNiIAVGRQKI0WyPw+6W0ceDJhWzrR4Gy7p2zp+2hAH5JNTD/1YjCCR82g==","repository":{"url":"git+https://github.com/AdguardTeam/re2-wasm.git","type":"git"},"_npmVersion":"11.16.0","description":"Google's RE2 library distributed as a WASM module patched by AdGuard.","directories":{},"_nodeVersion":"24.18.0","_hasShrinkwrap":false,"devDependencies":{"gts":"^3.1.0","heya-unit":"^0.3.0","typescript":"~4.1.3","@types/node":"^14.11.2","markdownlint":"0.34.0","markdownlint-cli":"0.41.0"},"_npmOperationalInternal":{"tmp":"tmp/re2-wasm_1.2.1_1784037160885_0.4661232003588325","host":"s3://npm-registry-packages-npm-production"}}},"time":{"created":"2024-06-19T15:27:06.210Z","modified":"2026-08-19T10:43:34.760Z","1.2.0":"2024-06-19T15:27:06.611Z","1.2.1":"2026-07-14T13:52:41.071Z"},"bugs":{"url":"https://github.com/AdguardTeam/re2-wasm/issues"},"license":"Apache-2.0","homepage":"https://github.com/AdguardTeam/re2-wasm","keywords":["regexp","re2","wasm"],"repository":{"url":"git+https://github.com/AdguardTeam/re2-wasm.git","type":"git"},"description":"Google's RE2 library distributed as a WASM module patched by AdGuard.","maintainers":[{"email":"am@adguard.com","name":"ameshkov"},{"email":"maximtop@gmail.com","name":"maximtop"},{"email":"vlad.abdulmianov@gmail.com","name":"blakhard"},{"email":"d.naumov@adguard.com","name":"d13410n3"}],"readme":"# @adguard/re2-wasm [![NPM version][npm-img]][npm-url]\n\n[npm-img]: https://img.shields.io/npm/v/@adguard/re2-wasm.svg\n[npm-url]: https://npmjs.org/package/@adguard/re2-wasm\n\n> Google's RE2 library distributed as a WASM module patched by AdGuard.\n\n> **Note:** This package is developed in\n> [AdGuardSoftwareLimited/ext-re2-wasm](https://github.com/AdGuardSoftwareLimited/ext-re2-wasm).\n> The [AdguardTeam/re2-wasm](https://github.com/AdguardTeam/re2-wasm)\n> repository is a public mirror.\n\n## Description\n\n**@adguard/re2-wasm** is a fork of Google's [re2-wasm](https://github.com/google/re2-wasm) that adds a configurable\nmaximum memory limit for regular expressions. It compiles Google's [RE2](https://github.com/google/re2) C++ library to\nWASM via Emscripten and exposes it as a drop-in replacement for JavaScript's `RegExp`.\n\nThis library is for JavaScript and TypeScript developers who need to handle user-supplied regular expressions\nsafely. The built-in `RegExp` engine can run in exponential time with a vulnerable regular expression and\n\"evil input\", leading to [Regular Expression Denial of Service\n(ReDoS)](https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS). RE2 guarantees\nlinear-time matching, protecting your application from ReDoS attacks. The AdGuard fork adds bounded memory\nusage via the `maxMem` option — the engine throws an error if a match attempt exceeds the configured memory\nlimit, preventing runaway resource consumption.\n\n`RE2`'s regular expression language is almost a superset of what is provided by `RegExp`\n(see [Syntax](https://github.com/google/re2/wiki/Syntax)), but it lacks two features: backreferences and lookahead\nassertions. See [Differences from RegExp](#differences-from-regexp) for details.\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Quick Start](#quick-start)\n- [API Overview](#api-overview)\n- [Usage Examples](#usage-examples)\n- [Differences from RegExp](#differences-from-regexp)\n- [Documentation](#documentation)\n\n---\n\n## Installation\n\n```bash\nnpm install @adguard/re2-wasm\n```\n\nRequires Node.js ≥ 10 or a browser with WASM support. The package has zero runtime dependencies — all functionality\ncomes from the compiled WASM module.\n\n## Quick Start\n\n```js\nconst { RE2 } = require('@adguard/re2-wasm');\n\nconst re = new RE2('hello (\\\\w+)', 'u');\nconst result = re.exec('hello world');\n// result[0]: 'hello world'\n// result[1]: 'world'\n// result.index: 0\nconsole.log(result[0]); // 'hello world'\n```\n\nOr with ES modules:\n\n```js\nimport { RE2 } from '@adguard/re2-wasm';\n\nconst re = new RE2('\\\\d+', 'gu');\n'abc 123 def 456'.match(re); // ['123', '456']\n```\n\n## API Overview\n\nThe `RE2` class emulates the standard `RegExp` interface. It can be used as a drop-in replacement in most cases.\n\n### Constructor\n\n```ts\nnew RE2(pattern: string | RegExp | RE2, flags?: string, maxMem?: number)\n```\n\n| Parameter | Type                         | Description                                                                                                 |\n| ---       | ---                          | ---                                                                                                         |\n| `pattern` | `string`, `RegExp`, or `RE2` | The regular expression pattern. Accepts existing `RegExp` or `RE2` objects.                                 |\n| `flags`   | `string`                     | Standard RegExp flags (`g`, `i`, `m`, `s`, `u`, `y`). The `u` flag is **required**.                         |\n| `maxMem`  | `number`                     | Maximum memory in bytes the regex engine can use. Defaults to `0` (no limit). If exceeded, throws an error. |\n\n### Properties\n\n| Property         | Type      | Description                                                             |\n| ---              | ---       | ---                                                                     |\n| `lastIndex`      | `number`  | Index at which to start the next match (used with `g` and `y` flags).   |\n| `global`         | `boolean` | Whether the `g` flag is set.                                            |\n| `ignoreCase`     | `boolean` | Whether the `i` flag is set.                                            |\n| `multiline`      | `boolean` | Whether the `m` flag is set.                                            |\n| `dotAll`         | `boolean` | Whether the `s` flag is set.                                            |\n| `unicode`        | `boolean` | Whether the `u` flag is set. Always `true`.                             |\n| `sticky`         | `boolean` | Whether the `y` flag is set.                                            |\n| `source`         | `string`  | The original pattern string.                                            |\n| `flags`          | `string`  | The flags string.                                                       |\n| `internalSource` | `string`  | The pattern after translation to RE2 syntax (read-only, for debugging). |\n\n### Methods\n\n| Method                                                        | Description                                                                   |\n| ---                                                           | ---                                                                           |\n| `exec(str: string): RE2ExecArray \\| null`                     | Executes a search for a match. Returns a match array or `null`.               |\n| `test(str: string): boolean`                                  | Tests for a match. Returns `true` or `false`.                                 |\n| `toString(): string`                                          | Returns the regex as a string (`/pattern/flags`).                             |\n| `match(str: string): RE2MatchArray \\| null`                   | Matches the string against the regex. Equivalent to `String.prototype.match`. |\n| `search(str: string): number`                                 | Searches for a match. Equivalent to `String.prototype.search`.                |\n| `replace(str: string, replacer: string \\| function): string`  | Replaces matches. Equivalent to `String.prototype.replace`.                   |\n| `split(str: string, limit?: number): (string \\| undefined)[]` | Splits the string. Equivalent to `String.prototype.split`.                    |\n\nThe `RE2` class also supports well-known symbols, so `String` methods work directly:\n\n```js\nconst re = new RE2('\\\\d+', 'u');\n'abc 123'.match(re);        // ['123', index: 4, input: 'abc 123']\n'abc 123'.search(re);       // 4\n'abc 123'.replace(re, 'X'); // 'abc X'\n'abc 123'.split(re);        // ['abc ', '']\n```\n\n## Usage Examples\n\n### Constructing from a RegExp object\n\n```js\nconst orig = /\\w+/gu;\nconst re = new RE2(orig);\n// Flags and pattern are copied from the original\nconsole.log(re.flags); // 'gu'\n```\n\n### Using the maxMem option to bound memory\n\n```js\n// Limit the regex engine to 4 KB of memory\nconst re = new RE2('a*b*c*d*e*f*g*h*i*j*k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z*', 'u', 4096);\ntry {\n  re.exec('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');\n} catch (e) {\n  console.log('Pattern exceeded memory limit');\n}\n```\n\n### Named capture groups\n\n```js\nconst re = new RE2('(?P<word>\\\\w+) (?P<number>\\\\d+)', 'u');\nconst result = re.exec('hello 42');\nconsole.log(result.groups.word);   // 'hello'\nconsole.log(result.groups.number); // '42'\n```\n\n### Using Symbol.matchAll\n\n```js\nconst re = new RE2('\\\\w+', 'gu');\nfor (const match of 'one two three'.matchAll(re)) {\n  console.log(match[0]);\n}\n// 'one'\n// 'two'\n// 'three'\n```\n\n### Replacing with a function\n\n```js\nconst re = new RE2('(\\\\d+)', 'gu');\nconst result = re.replace('a 10 b 20 c 30', (match, n) => String(Number(n) * 2));\nconsole.log(result); // 'a 20 b 40 c 60'\n```\n\n### Falling back to RegExp for unsupported patterns\n\n```js\nconst pattern = /(a)+(b)*\\\\1/u;\nlet re;\ntry {\n  re = new RE2(pattern);\n} catch (e) {\n  // Pattern uses backreferences — fall back to RegExp\n  re = pattern;\n}\nconst result = re.exec('aabbaabb');\n```\n\n## Differences from RegExp\n\n### Backreferences and lookahead assertions not supported\n\n`RE2` does not support backreferences (`\\\\1`, `\\\\2`, etc.) or lookahead/lookbehind assertions (`(?=...)`, `(?<=...)`,\n`(?!...)`, `(?<!...)`). Attempting to use these features throws a `SyntaxError`. If your patterns require them,\nfall back to `RegExp` (see the [fallback example](#falling-back-to-regexp-for-unsupported-patterns) above).\n\n### Unicode flag is mandatory\n\nThe `RE2` engine always works in Unicode mode. The `u` flag must be passed when constructing an `RE2` instance:\n\n```js\nnew RE2('\\\\w+');           // throws Error: \"u\" flag must be passed\nnew RE2('\\\\w+', 'u');      // OK\n```\n\n### Memory limit\n\nThe `maxMem` constructor parameter (an AdGuard extension) restricts how much memory the regex engine can allocate\nduring matching. If the limit is exceeded, the engine throws an error. Pass `0` (the default) for no limit.\n\n### Dot behavior\n\nIn RE2, `.` matches any character including `\\\\n`, regardless of whether the `m` or `s` flags are set. This is\nequivalent to `RegExp`'s `s` (dotAll) flag always being on:\n\n```js\nconst re = new RE2('a.b', 'u');\nconsole.log(re.exec('a\\\\nb')); // ['a\\\\nb']\nconsole.log(/a.b/u.exec('a\\\\nb')); // null\n```\n\n### Anchors in multiline mode\n\nIn multiline mode, `$` in RE2 does not match between `\\\\r` and `\\\\n` when the string ends with `\\\\r\\\\n`:\n\n```js\nconst re = new RE2('a$', 'mu');\nre.exec('a\\\\r\\\\n'); // null — RE2 does not match between \\\\r and \\\\n\n/a$/mu.exec('a\\\\r\\\\n'); // ['a'] — RegExp matches between \\\\r and \\\\n\n```\n\n### `compile()` method\n\n`RegExp.prototype.compile()` is deprecated and not implemented in `RE2`. Calling it throws an error. Create a new\n`RE2` instance instead.\n\n### `d` flag (hasIndices)\n\nThe `hasIndices` (`d`) flag is not supported. Use `RegExp` with the `d` flag if you need start and end indices of\ncapture groups.\n\n---\n\n## Documentation\n\n- [Development](DEVELOPMENT.md) — how to set up and contribute\n- [Changelog](CHANGELOG.md) — version history\n- [LLM agent rules](AGENTS.md) — AI-assisted development guidelines\n","readmeFilename":"README.md"}