regexpu-core
raw JSON → 6.4.0 verified Fri May 01 auth: no javascript
regexpu-core is the core module of regexpu, a transpiler that converts ES2015 Unicode regular expressions (with the `u` and `v` flags) into ES5-compatible patterns. Current stable version is 6.4.0. It supports features like Unicode property escapes, dotAll flag, named capture groups, and the `v` flag (unicodeSetsFlag), with options to either pass through or transform them. Released on npm with TypeScript types, it has a steady release cadence, updated for Unicode 15.1. Key differentiator: focused on core regexp rewriting without the full regexpu CLI, used by Babel and other tools.
Common errors
error TypeError: rewritePattern is not a function ↓
cause Using named import when default export expected in CJS.
fix
Use
const rewritePattern = require('regexpu-core'); error Can't find module 'regexpu-core/data/character-class-escape-sets' ↓
cause Importing internal modules directly, not part of public API.
fix
Do not import from internal paths; use only 'regexpu-core'.
error TypeScript error: 'RewritePatternOptions' is a type and must be imported using a type-only import ↓
cause Using regular import for a type-only export.
fix
Use
import type { RewritePatternOptions } from 'regexpu-core'; Warnings
breaking v6.0.0: Always enable unicodeSetsFlag parsing. Removed option to disable 'v' flag support. ↓
fix Remove any setting that disables 'v' flag; pattern is always parsed with 'v' flag support.
deprecated The 'useUnicodeFlag' option is deprecated since v5.0.0. ↓
fix Replace 'useUnicodeFlag' with 'unicodeFlag' option.
gotcha When using CommonJS require, the default export is the function, not an object. ↓
fix Use `const rewritePattern = require('regexpu-core');` not `require('...').rewritePattern`.
gotcha The 'unicodeFlag' option must be 'transform' to actually transform; false leaves it as-is. ↓
fix Set `unicodeFlag: 'transform'` explicitly to get ES5-compatible output.
gotcha Named capture groups are not transformed by default; must set 'namedGroups' option. ↓
fix Pass `namedGroups: 'transform'` option to compile named groups.
Install
npm install regexpu-core yarn add regexpu-core pnpm add regexpu-core Imports
- rewritePattern wrong
const rewritePattern = require('regexpu-core')correctimport rewritePattern from 'regexpu-core' - rewritePattern (named) wrong
import rewritePattern from 'regexpu-core'correctimport { rewritePattern } from 'regexpu-core' - types wrong
import { RewritePatternOptions } from 'regexpu-core'correctimport type { RewritePatternOptions } from 'regexpu-core'
Quickstart
import rewritePattern from 'regexpu-core';
// Transform a Unicode regex with 'u' flag to ES5
const pattern = '\\p{ASCII_Hex_Digit}+';
const flags = 'u';
const result = rewritePattern(pattern, flags, {
unicodePropertyEscapes: 'transform',
unicodeFlag: 'transform',
});
console.log(result);
// E.g.: '(?:[0-9A-Fa-f])+'