or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

@babel/plugin-transform-sticky-regex

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.

Package Information

  • Package Name: @babel/plugin-transform-sticky-regex
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install --save-dev @babel/plugin-transform-sticky-regex

Core Imports

// 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");

Basic Usage

Babel Configuration

Add the plugin to your Babel configuration:

{
  "plugins": ["@babel/plugin-transform-sticky-regex"]
}

Input/Output Example

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");

Capabilities

Default Export Plugin Function

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.

RegExp Literal Transformation

Transforms regular expression literals that contain the sticky flag ('y') into equivalent new RegExp() constructor calls.

Transformation Logic:

  • Detects regex literal nodes in the AST
  • Checks if the regex flags include the sticky flag ('y')
  • If sticky flag is present, replaces the literal with new RegExp(pattern, flags)
  • If no sticky flag, leaves the regex unchanged
  • Preserves all original flags and pattern content

Examples:

InputOutputReason
/abc/ynew RegExp("abc", "y")Has sticky flag
/abc/gynew RegExp("abc", "gy")Has sticky flag + global
/abc/yinew RegExp("abc", "yi")Has sticky flag + case insensitive
/abc/g/abc/gNo sticky flag, unchanged
/abc//abc/No flags, unchanged

Edge Cases:

  • Regex patterns with escape sequences are preserved exactly: /\w+/ynew RegExp("\\w+", "y")
  • Complex patterns remain unchanged: /(?:foo|bar)/ynew RegExp("(?:foo|bar)", "y")
  • Only sticky flag presence triggers transformation, other flags are preserved

Types

/** 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;
}

Compatibility

  • Babel Version: Requires Babel 7.0.0 or higher
  • Node.js: Requires Node.js 6.9.0 or higher
  • Browser Support: Transforms ES2015 sticky regex to work in ES5 environments
  • Target Environments: Any JavaScript environment that supports RegExp constructor

Dependencies

  • @babel/helper-plugin-utils: Provides plugin creation utilities and API polyfills
  • @babel/core: Peer dependency (^7.0.0-0) providing Babel transformation API and AST types

Peer 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.