Babel plugin that transforms ES2015 Unicode regular expressions to ES5-compatible syntax
npx @tessl/cli install tessl/npm-babel--plugin-transform-unicode-regex@7.27.0@babel/plugin-transform-unicode-regex is a Babel plugin that transforms ES2015 Unicode regular expressions (using the u flag) to ES5-compatible syntax. It enables modern Unicode regex features in environments that don't support ES2015, making it essential for maintaining backwards compatibility while using modern JavaScript regex patterns.
npm install --save-dev @babel/plugin-transform-unicode-regex// Default import (the plugin function)
import plugin from "@babel/plugin-transform-unicode-regex";For CommonJS:
const plugin = require("@babel/plugin-transform-unicode-regex");Note: This plugin only exports a single default export - the plugin function. There are no named exports.
Add the plugin to your Babel configuration:
{
"plugins": ["@babel/plugin-transform-unicode-regex"]
}Or in babel.config.js:
module.exports = {
plugins: ["@babel/plugin-transform-unicode-regex"]
};Input (ES2015 with Unicode flag):
var string = "foo💩bar";
var match = string.match(/foo(.)bar/u);Output (ES5-compatible):
var string = "foo💩bar";
var match = string.match(/foo((?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]))bar/);The main and only export of this package is the Babel plugin function.
/**
* Default export: Babel plugin factory function created with declare()
* The actual implementation uses declare() and createRegExpFeaturePlugin()
*/
declare const plugin: (
api: PluginAPI,
options?: any,
dirname?: string
) => PluginObject;
export default plugin;
interface PluginAPI {
assertVersion(range: number | string): void;
version: string;
// Additional properties available but not used by this specific plugin
}
interface PluginObject {
name: string;
manipulateOptions?: (opts: any, parserOpts: any) => void;
pre?: (file: any) => void;
post?: (file: any) => void;
visitor?: {
[key: string]: (path: any, state: any) => void;
};
}The plugin is created using Babel's helper utilities and specifically targets the Unicode flag feature:
/pattern/u)api.assertVersion(REQUIRED_VERSION(7)))The source code shows the plugin is implemented as:
import { createRegExpFeaturePlugin } from "@babel/helper-create-regexp-features-plugin";
import { declare } from "@babel/helper-plugin-utils";
export default declare(api => {
api.assertVersion(REQUIRED_VERSION(7));
return createRegExpFeaturePlugin({
name: "transform-unicode-regex",
feature: "unicodeFlag",
});
});The plugin is built using Babel's official helper libraries:
createRegExpFeaturePlugindeclare function for plugin creationdeclare()The plugin implementation is minimal - it uses createRegExpFeaturePlugin to handle all the complex Unicode-to-ES5 transformation logic. The plugin configuration specifies:
u flag specifically)This leverages the shared regex transformation infrastructure that multiple Babel regex plugins use, ensuring consistent behavior and optimal performance.
This plugin is automatically included in Babel presets like @babel/preset-env when targeting environments that don't support ES2015 Unicode regex. It can also be used individually when you need specific Unicode regex transformation without other ES2015+ transformations.
Target Environments:
Use Cases: