Compile regular expressions using named groups to ES5.
npx @tessl/cli install tessl/npm-babel--plugin-transform-named-capturing-groups-regex@7.27.0A Babel plugin that transforms JavaScript regular expressions using named capturing groups into ES5-compatible syntax. It provides compile-time transformation of regex patterns containing named groups (like /(?<name>\w+)/) into equivalent ES5 regex syntax with numeric groups, ensuring compatibility with older JavaScript environments.
npm install --save-dev @babel/plugin-transform-named-capturing-groups-regex// ESM
import plugin from "@babel/plugin-transform-named-capturing-groups-regex";
// CommonJS
const plugin = require("@babel/plugin-transform-named-capturing-groups-regex");Add the plugin to your Babel configuration:
{
"plugins": ["@babel/plugin-transform-named-capturing-groups-regex"]
}{
"plugins": [
["@babel/plugin-transform-named-capturing-groups-regex", {
"runtime": false
}]
]
}Input (ES2018+ named groups):
const dateRegex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const result = dateRegex.exec("2023-12-25");
console.log(result.groups.year); // "2023"
console.log(result.groups.month); // "12"
console.log(result.groups.day); // "25"Output (ES5 compatible):
const dateRegex = /(\d{4})-(\d{2})-(\d{2})/;
const result = dateRegex.exec("2023-12-25");
// Runtime support provides .groups property access
console.log(result.groups.year); // "2023"
console.log(result.groups.month); // "12"
console.log(result.groups.day); // "25"The main plugin export provides a Babel plugin for transforming named capturing groups in regular expressions.
/**
* Default export: Babel plugin for transforming named capturing groups
* @param api - Babel API object
* @param options - Plugin configuration options
* @returns Babel plugin object with transformation logic
*/
declare function plugin(api: any, options?: PluginOptions): any;
export default plugin;
/**
* Plugin configuration options
*/
interface PluginOptions {
/** Enable runtime mode for dynamic regex patterns (default: false) */
runtime?: boolean;
}Option Details:
runtime (optional, boolean): When set to true, enables runtime mode for handling dynamic regex patterns. In runtime mode, the plugin includes additional runtime support for transforming patterns created at runtime. Defaults to false for compile-time only transformation.The plugin validates configuration options and throws errors for invalid input:
// Throws Error if runtime option is not a boolean
throw new Error("The 'runtime' option must be boolean");Common Error Scenarios:
runtime optionEnable runtime mode for applications that create regex patterns dynamically:
{
"plugins": [
["@babel/plugin-transform-named-capturing-groups-regex", {
"runtime": true
}]
]
}This mode provides additional runtime support for regex patterns that cannot be analyzed at compile time.
The plugin works seamlessly with other Babel regex plugins and should be ordered appropriately in your plugin chain:
{
"plugins": [
"@babel/plugin-transform-unicode-regex",
"@babel/plugin-transform-named-capturing-groups-regex",
"@babel/plugin-transform-sticky-regex"
]
}This plugin enables named capturing groups to work in environments that don't support ES2018 regex features:
This plugin is part of the Babel RegExp features plugin system and automatically handles:
.groups property polyfillThe plugin integrates seamlessly with other Babel regex transformation plugins and preset-env.