Registry / serialization / pcre-to-regexp

pcre-to-regexp

JSON →
library1.1.0jsnpmunverified

pcre-to-regexp is a utility library designed to convert Perl Compatible Regular Expression (PCRE) strings into native JavaScript RegExp instances. It supports parsing PCRE patterns, including handling delimiters and flags, and can extract named capture group keys. The current stable version is 1.1.0. The project maintains a somewhat irregular release cadence, with the last major update (v1.0.0) occurring relatively recently, primarily focusing on a refactor to TypeScript for improved type safety and maintainability. Its key differentiator lies in its specialized focus on PCRE conversion, which is useful for migrating regular expressions from environments like PHP (which heavily uses PCRE) to JavaScript, although it notes it's not yet feature-complete for all PCRE constructs.

npm install pcre-to-regexp
INSTALL
IMPORT
SIG · PCRE-TO-REGEXP
P
pcre-to-regexp
serializationjavascriptv1.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PCRE
import PCRE from 'pcre-to-regexp';
const PCRE = require('pcre-to-regexp');
The library primarily exports a default function. Since v1.0.0, types are shipped, supporting TypeScript's default import syntax. The CJS require is also common for older Node.js projects.
PCRE (TypeScript type)
import type { PCRE } from 'pcre-to-regexp';
import { PCRE } from 'pcre-to-regexp';
While the main export is a default function, the type definitions are exported as named exports within a namespace since v1.1.0.

This example demonstrates how to convert a PCRE string to a JavaScript RegExp, extract named capture group keys, and then use the resulting RegExp to match a string, showcasing access to named captures.

import PCRE from 'pcre-to-regexp'; const url = '%^https?://twitter\\.com(/\\#\\!)?/(?P<username>[a-zA-Z0-9_]{1,20})\\/status(es)?/(?P<id>\\d+)/?$%ig'; // parse the PCRE regexp into a JS RegExp const keys = []; const re = PCRE(url, keys); console.log('Extracted keys:', keys); // Expected: [ , 'username', , 'id' ] console.log('Resulting RegExp:', re); // Expected: /^https?:\/\/twitter\\.com(\/\\#\\!)?\/([a-zA-Z0-9_]{1,20})\\/status(es)?\/(\\d+)\/?)?$/gi const match = re.exec('https://twitter.com/tootallnate/status/481604870626349056'); console.log('Match result:', match); // Example of copying named captures to the match object if (match) { for (let i = 0; i < keys.length; i++) { if (typeof keys[i] === 'string') { match[keys[i]] = match[i + 1]; } } console.log('Username from named capture:', match.username); console.log('ID from named capture:', match.id); } else { console.log('No match found.'); }
Debug
Known issues
breakingVersion 1.0.0 introduced a significant refactor to TypeScript, which might cause subtle type-related issues or require minor adjustments in existing TypeScript projects.
fix
Ensure your TypeScript configuration is up-to-date and re-evaluate any type assertions or inferences if upgrading from pre-1.0.0 versions.
affects: >=1.0.0
gotchaThe library is not fully feature-complete for all PCRE constructs. Complex or very specific PCRE features might not be correctly translated, leading to unexpected behavior or incorrect RegExp patterns.
fix
Thoroughly test the generated JavaScript RegExp with a comprehensive suite of test cases, especially for complex PCRE patterns, to ensure accurate translation. Consult the project's issues for known limitations.
affects: >=0.0.1
gotchaNamed capture groups are not automatically added as properties to the `RegExpMatchArray` object returned by `RegExp.prototype.exec()`. A manual iteration over the `keys` array is required.
fix
After obtaining the `match` array and `keys` array, iterate through `keys` and manually assign `match[i + 1]` to `match[keys[i]]` for each named capture, as shown in the quickstart example.
affects: >=0.0.1
gotchaIncorrect handling of PCRE delimiters (e.g., unmatched delimiters or delimiters appearing within the pattern without proper escaping) can lead to parsing errors or malformed JavaScript RegExp objects.
fix
Ensure PCRE strings adhere to standard PCRE syntax, with consistent delimiters (e.g., `%...%`, `/.../`) and proper escaping of delimiters if they appear within the pattern itself.
affects: >=0.0.1
Errors
Common errors & fixes
TypeError: PCRE is not a function
Attempting to use `require('pcre-to-regexp')` and then directly calling `PCRE()`, but the CommonJS import might resolve differently in some environments, or in ESM contexts, a default import was not used.
fix
For CommonJS, try `const PCRE = require('pcre-to-regexp').default;` or ensure your module resolver correctly handles default exports from ESM. For ESM, use `import PCRE from 'pcre-to-regexp';`.
TS2305: Module '"pcre-to-regexp"' has no exported member 'PCRE'.
Attempting to import `PCRE` as a named export when it's primarily a default export, or trying to import a type that is within a namespace without the correct syntax.
fix
For the default function, use `import PCRE from 'pcre-to-regexp';`. For types, ensure you are using `import type { PCRE } from 'pcre-to-regexp';` as of v1.1.0, specifically for type-related imports.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
pcre-to-regexp — npm install pcre-to-regexp · libregistry