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-regexpVerified import paths — ran on the pinned version, not inferred.
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.
Ensure your TypeScript configuration is up-to-date and re-evaluate any type assertions or inferences if upgrading from pre-1.0.0 versions.
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.
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.
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.
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';`.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.No dependency data recorded yet.