Babel Helper Regex provides utility functions for working with regular expression literals in Babel transformations. It operates on Babel AST nodes representing RegExp literals and helps with flag manipulation during JavaScript transpilation workflows.
npm install babel-helper-regexES6 modules:
import * as regex from "babel-helper-regex";
import { is, pullFlag } from "babel-helper-regex";CommonJS:
const regex = require("babel-helper-regex");
const { is, pullFlag } = require("babel-helper-regex");import * as regex from "babel-helper-regex";
import * as t from "babel-types";
// Example: Check if a RegExp literal has the unicode flag
export default function () {
return {
visitor: {
RegExpLiteral({ node }) {
// Check if node has unicode flag
if (regex.is(node, "u")) {
// Process the node with unicode flag
console.log("Found unicode regex:", node.pattern);
// Remove the unicode flag after processing
regex.pullFlag(node, "u");
}
}
}
};
}Checks if a RegExp literal node contains a specific flag.
/**
* Checks if a node is a RegExp literal with a specific flag
* @param {Object} node - Babel AST node to check (must be RegExp literal)
* @param {string} flag - The flag to look for ('g', 'i', 'm', 's', 'u', 'y')
* @returns {boolean} true if the node is a RegExp literal and contains the specified flag
*/
function is(node, flag);Usage Examples:
import * as regex from "babel-helper-regex";
import * as t from "babel-types";
// Create a RegExp literal node for testing
const regexNode = t.regExpLiteral("test", "gui");
// Check for different flags
console.log(regex.is(regexNode, "g")); // true
console.log(regex.is(regexNode, "u")); // true
console.log(regex.is(regexNode, "y")); // false
// Works with any valid RegExp flags
console.log(regex.is(regexNode, "i")); // true (ignoreCase)
console.log(regex.is(regexNode, "m")); // false (multiline)
console.log(regex.is(regexNode, "s")); // false (dotAll)Removes a flag from a RegExp literal node by mutating the node in place.
/**
* Removes a flag from a RegExp literal node
* @param {Object} node - Babel AST node representing a RegExp literal
* @param {string} flag - The flag to remove ('g', 'i', 'm', 's', 'u', 'y')
* @returns {void} Mutates the node.flags property in place
*/
function pullFlag(node, flag);Usage Examples:
import * as regex from "babel-helper-regex";
import * as t from "babel-types";
// Create a RegExp literal with multiple flags
const regexNode = t.regExpLiteral("pattern", "guiy");
console.log(regexNode.flags); // "guiy"
// Remove the unicode flag
regex.pullFlag(regexNode, "u");
console.log(regexNode.flags); // "giy"
// Remove global flag
regex.pullFlag(regexNode, "g");
console.log(regexNode.flags); // "iy"
// Removing non-existent flag does nothing
regex.pullFlag(regexNode, "m");
console.log(regexNode.flags); // "iy" (unchanged)/**
* Babel AST node representing a regular expression literal
* This is provided by babel-types package
*/
interface RegExpLiteral {
type: "RegExpLiteral";
pattern: string; // The regex pattern without delimiters
flags: string; // String containing flag characters (e.g., "gui")
}
/**
* Valid RegExp flag characters
*/
type RegExpFlag = "g" | "i" | "m" | "s" | "u" | "y";Both functions expect valid Babel AST nodes:
is() returns false if the node is not a RegExp literal (uses babel-types.isRegExpLiteral() internally)pullFlag() will have no effect if the flag doesn't exist in the node's flagsflags property when it is a RegExp literalThis package requires:
lodash/pull for array manipulation during flag removalWhen using this package, ensure these dependencies are available in your project.