regexpu
raw JSON → 4.8.0 verified Fri May 01 auth: no javascript
regexpu is a source code transpiler that enables the use of ES2015 Unicode regular expressions (the `u` flag) in ES5 environments. It rewrites regex literals with the `u` flag into equivalent ES5-compatible patterns, handling Unicode properties, escapes, and case folding. Current version 4.8.0 (stable) releases infrequently; major version bumps (e.g., 2.0.0) introduced breaking changes in module export paths. Key differentiators: it is the core engine behind Babel, Traceur, and other transpilers for Unicode regex; also ships `regexpu-core` for programmatic use. Supports Node >=6 and browsers via bundlers.
Common errors
error require is not defined ↓
cause Trying to use CommonJS require in an ESM module (e.g., .mjs file or type:module).
fix
Use import statements: import { rewritePattern } from 'regexpu-core';
error Cannot find module 'regexpu' or 'regexpu-core' ↓
cause Module not installed or incorrect import path.
fix
Install the correct package: npm install regexpu --save (or regexpu-core if using core).
error rewritePattern is not a function ↓
cause Import destructuring error; default export vs named export confusion.
fix
Use named import: import { rewritePattern } from 'regexpu-core';
Warnings
breaking Breaking change in v2.0.0: require('regexpu-core') is the new require('regexpu/rewrite-pattern') ↓
fix Update imports: use require('regexpu-core') or import { rewritePattern } from 'regexpu-core' instead of old require('regexpu').rewritePattern.
deprecated Using require('regexpu') directly may be deprecated; prefer regexpu-core submodule. ↓
fix Switch to require('regexpu-core') for the rewritePattern function.
gotcha The package only transpiles the regex pattern string; it does not handle the regular expression literal syntax. Pass the pattern without delimiters. ↓
fix Ensure input is a string pattern (e.g., '\\p{ASCII_Hex_Digit}') not '/\\p{ASCII_Hex_Digit}/u'.
gotcha Options object must be passed correctly: { unicodePropertyEscape: true } to enable Unicode property escapes (default false). ↓
fix Pass options as second or third argument to rewritePattern.
Install
npm install regexpu yarn add regexpu pnpm add regexpu Imports
- rewritePattern wrong
const rewritePattern = require('regexpu').rewritePatterncorrectimport { rewritePattern } from 'regexpu' - default wrong
const regexpu = require('regexpu')correctimport regexpu from 'regexpu' - rewritePattern (from regexpu-core) wrong
const rewritePattern = require('regexpu').rewritePatterncorrectimport { rewritePattern } from 'regexpu-core'
Quickstart
import { rewritePattern } from 'regexpu-core';
// Transpile a regex with the u flag to ES5
const pattern = '\\p{ASCII_Hex_Digit}';
const flags = 'u';
const result = rewritePattern(pattern, flags);
console.log(result); // '[A-Fa-f0-9]'
// With support for Unicode properties
const pattern2 = '\\p{Emoji}';
const flags2 = 'u';
const result2 = rewritePattern(pattern2, flags2, { unicodePropertyEscape: true });
console.log(result2); // '[...]'