php-regex / regex-transpiler
Transpiles PCRE patterns to JavaScript and Python regular expressions, with the losses reported.
Fund package maintenance!
Requires
- php: >=8.2
- php-regex/regex-parser: ^2.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-04 21:45:36 UTC
README
PHPRegex Transpiler
Transpiles PCRE patterns to JavaScript and Python regular expressions, with the losses reported.
Features
- Two targets,
javascript(aliasjs) andpython(aliaspy), each producing a paste-ready literal and a constructor call - Dialect rewrites:
(?P<name>)becomes(?<name>)in JavaScript,\k<name>becomes(?P=name)in Python,\hand\vbecome character classes, atomic groups are emulated in Python - Flags are mapped per target: JavaScript keeps
i,m,s,u, gainsuwhen Unicode escapes need it, and has/xapplied then dropped; Python keepsi,m,s,xand spells flags inline or asreconstants - Warnings list every rewrite to review; notes list run-time caveats, such as ASCII-based
\win JavaScript - A construct the target cannot express — a possessive quantifier,
\p{...}in Python — throws aTranspileExceptioncarrying the position in the pattern
Installation
composer require php-regex/regex-transpiler
PHP 8.2 or newer. php-regex/regex-parser is pulled in automatically.
Configuration
transpile() takes an optional TranspileOptions object: allowLookbehind
(bool, default true) — false makes lookbehind throw for JavaScript targets.
Usage
A PCRE pattern with a named group and a backreference, for a JavaScript codebase:
use PHPRegex\Parser\RegexParser; use PHPRegex\Transpiler\Transpiler; $transpiler = new Transpiler(RegexParser::create()); $result = $transpiler->transpile('/(?P<word>\w+)\k{word}/i', 'javascript'); echo $result->literal; // /(?<word>\w+)\k<word>/i echo $result->notes[0]; // JavaScript \w and \b are ASCII-based; Unicode word boundaries may differ.
For Python, the literal is a raw string carrying the flags inline, and the constructor is a re.compile() call:
$result = $transpiler->transpile('/(?P<word>\w+)\k<word>/i', 'python'); echo $result->literal; // r'(?i)(?P<word>\w+)(?P=word)' echo $result->constructor; // re.compile(r'(?P<word>\w+)(?P=word)', re.IGNORECASE)
Every rewrite is reported, and what a target cannot express is refused rather than approximated:
$result = $transpiler->transpile('/\p{L}+/', 'javascript'); echo $result->literal; // /\p{L}+/u echo $result->warnings[0]; // Added /u for Unicode property escapes. try { $transpiler->transpile('/a++/', 'javascript'); } catch (\PHPRegex\Transpiler\TranspileException $e) { echo $e->getMessage(); // Possessive quantifiers are not supported in JavaScript. }
Documentation
- API reference — the
transpile()entry point and theTranspileResultfields - CLI guide — the
regex transpilecommand, its--targetoption and its exit codes - Backward compatibility promise — what stays stable across releases
Resources
- Documentation
- The parsing core it builds on: regex-parser
- Changelog
- Report issues and send pull requests in the main PHPRegex repository
Sponsors
If PHPRegex saves you time, consider sponsoring its maintenance.
License
MIT. See LICENSE.