A Babel plugin that transforms ES2015 sticky regular expressions (using the 'y' flag) into ES5-compatible RegExp constructor calls. This plugin provides automatic backward compatibility for sticky regex patterns in older JavaScript environments.
npm install --save-dev @babel/plugin-transform-sticky-regex// As a Babel plugin in .babelrc or babel.config.js
{
"plugins": ["@babel/plugin-transform-sticky-regex"]
}For programmatic use:
import transformStickyRegex from "@babel/plugin-transform-sticky-regex";
import { types as t } from "@babel/core";CommonJS:
const transformStickyRegex = require("@babel/plugin-transform-sticky-regex");
const { types: t } = require("@babel/core");Add the plugin to your Babel configuration:
{
"plugins": ["@babel/plugin-transform-sticky-regex"]
}Input (ES2015 with sticky regex):
var re = /o+/y;
var pattern = /[a-z]+/gy;Output (ES5 compatible):
var re = new RegExp("o+", "y");
var pattern = new RegExp("[a-z]+", "gy");The main plugin function that integrates with Babel's transformation pipeline.
/**
* Babel plugin that transforms sticky regex literals to RegExp constructor calls
* @param api - Babel plugin API containing version checking and AST types
* @returns Babel plugin object with visitor pattern for regex transformation
*/
declare function transformStickyRegex(api: PluginAPI): PluginObject;
interface PluginObject {
/** Plugin identifier for Babel */
name: "transform-sticky-regex";
/** AST visitor containing transformation logic */
visitor: {
RegExpLiteral(path: NodePath<RegExpLiteral>): void;
};
}The plugin function is created using @babel/helper-plugin-utils' declare wrapper, which provides API version validation and compatibility polyfills. The plugin validates it's running with Babel 7.0.0 or higher using api.assertVersion() with a version requirement constant.
Transforms regular expression literals that contain the sticky flag ('y') into equivalent new RegExp() constructor calls.
Transformation Logic:
new RegExp(pattern, flags)Examples:
| Input | Output | Reason |
|---|---|---|
/abc/y | new RegExp("abc", "y") | Has sticky flag |
/abc/gy | new RegExp("abc", "gy") | Has sticky flag + global |
/abc/yi | new RegExp("abc", "yi") | Has sticky flag + case insensitive |
/abc/g | /abc/g | No sticky flag, unchanged |
/abc/ | /abc/ | No flags, unchanged |
Edge Cases:
/\w+/y → new RegExp("\\w+", "y")/(?:foo|bar)/y → new RegExp("(?:foo|bar)", "y")/** Babel plugin API interface */
interface PluginAPI {
/** Check Babel version compatibility */
assertVersion(version: number | string): void;
/** Babel AST types and constructors */
types: typeof import("@babel/types");
/** Current Babel version */
version: string;
}
/** AST node path for regex literals */
interface NodePath<T> {
/** The AST node being visited */
node: T;
/** Replace current node with new AST node */
replaceWith(node: any): void;
}
/** Regular expression literal AST node */
interface RegExpLiteral {
/** Regex pattern string */
pattern: string;
/** Regex flags string (e.g., "gy", "i", "m") */
flags: string;
}RegExp constructorPeer Dependency Requirements:
This plugin requires @babel/core as a peer dependency. Make sure you have Babel 7.0.0 or higher installed in your project. The plugin will validate this requirement at runtime and throw an error if an incompatible version is detected.