{"id":15754,"library":"pcre-to-regexp","title":"PCRE to JavaScript RegExp Converter","description":"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.","status":"active","version":"1.1.0","language":"javascript","source_language":"en","source_url":"git://github.com/TooTallNate/pcre-to-regexp","tags":["javascript","pcre","regexp","convert","js","php","preg","preg_replace","preg_match","typescript"],"install":[{"cmd":"npm install pcre-to-regexp","lang":"bash","label":"npm"},{"cmd":"yarn add pcre-to-regexp","lang":"bash","label":"yarn"},{"cmd":"pnpm add pcre-to-regexp","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"const PCRE = require('pcre-to-regexp');","symbol":"PCRE","correct":"import PCRE from 'pcre-to-regexp';"},{"note":"While the main export is a default function, the type definitions are exported as named exports within a namespace since v1.1.0.","wrong":"import { PCRE } from 'pcre-to-regexp';","symbol":"PCRE (TypeScript type)","correct":"import type { PCRE } from 'pcre-to-regexp';"}],"quickstart":{"code":"import PCRE from 'pcre-to-regexp';\n\nconst url = '%^https?://twitter\\\\.com(/\\\\#\\\\!)?/(?P<username>[a-zA-Z0-9_]{1,20})\\\\/status(es)?/(?P<id>\\\\d+)/?$%ig';\n\n// parse the PCRE regexp into a JS RegExp\nconst keys = [];\nconst re = PCRE(url, keys);\n\nconsole.log('Extracted keys:', keys);\n// Expected: [ , 'username', , 'id' ]\n\nconsole.log('Resulting RegExp:', re);\n// Expected: /^https?:\\/\\/twitter\\\\.com(\\/\\\\#\\\\!)?\\/([a-zA-Z0-9_]{1,20})\\\\/status(es)?\\/(\\\\d+)\\/?)?$/gi\n\nconst match = re.exec('https://twitter.com/tootallnate/status/481604870626349056');\nconsole.log('Match result:', match);\n\n// Example of copying named captures to the match object\nif (match) {\n  for (let i = 0; i < keys.length; i++) {\n    if (typeof keys[i] === 'string') {\n      match[keys[i]] = match[i + 1];\n    }\n  }\n  console.log('Username from named capture:', match.username);\n  console.log('ID from named capture:', match.id);\n} else {\n  console.log('No match found.');\n}","lang":"typescript","description":"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."},"warnings":[{"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.","message":"Version 1.0.0 introduced a significant refactor to TypeScript, which might cause subtle type-related issues or require minor adjustments in existing TypeScript projects.","severity":"breaking","affected_versions":">=1.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=0.0.1"},{"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.","message":"Named 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.","severity":"gotcha","affected_versions":">=0.0.1"},{"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.","message":"Incorrect 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.","severity":"gotcha","affected_versions":">=0.0.1"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"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';`.","cause":"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.","error":"TypeError: PCRE is not a function"},{"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.","cause":"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.","error":"TS2305: Module '\"pcre-to-regexp\"' has no exported member 'PCRE'."}],"ecosystem":"npm"}