@babel/plugin-transform-dotall-regex is a Babel plugin that transforms JavaScript regular expressions using the dotAll flag (s flag) to make them compatible with ES5 environments. The dotAll flag allows the dot metacharacter (.) in regular expressions to match newline characters, which is not supported in older JavaScript engines.
npm install --save-dev @babel/plugin-transform-dotall-regexThis is a Babel plugin, so it's typically used in Babel configuration rather than directly imported:
{
"plugins": ["@babel/plugin-transform-dotall-regex"]
}For programmatic usage with Babel:
import plugin from "@babel/plugin-transform-dotall-regex";
import { declare } from "@babel/helper-plugin-utils";
import { createRegExpFeaturePlugin } from "@babel/helper-create-regexp-features-plugin";CommonJS:
const plugin = require("@babel/plugin-transform-dotall-regex");
const { declare } = require("@babel/helper-plugin-utils");
const { createRegExpFeaturePlugin } = require("@babel/helper-create-regexp-features-plugin");Add the plugin to your .babelrc or babel.config.js:
{
"plugins": ["@babel/plugin-transform-dotall-regex"]
}Input (ES2018 with dotAll flag):
var a = /./; // Matches any character except newlines
var b = /./s; // Matches any character including newlines (dotAll)Output (ES5 compatible):
var a = /./; // Unchanged - no dotAll flag
var b = /[^]/; // Transformed - [^] matches any character including newlinesimport { transform } from "@babel/core";
import plugin from "@babel/plugin-transform-dotall-regex";
const code = `
const regex = /pattern./s;
const multiFlag = /test./gisu;
`;
const result = transform(code, {
plugins: [plugin]
});
console.log(result.code);
// Output:
// const regex = /pattern[^]/;
// const multiFlag = /test[^]/giu;The main export is a Babel plugin factory function that creates a plugin instance configured for dotAll regex transformation.
/**
* Creates a Babel plugin for transforming dotAll regex patterns
* @param api - Babel API object with assertVersion method and other utilities
* @returns Babel plugin object configured for dotAll transformation
*/
export default declare(api => {
api.assertVersion(REQUIRED_VERSION(7));
return createRegExpFeaturePlugin({
name: "transform-dotall-regex",
feature: "dotAllFlag",
});
});
// Dependencies - imported from helper packages
function createRegExpFeaturePlugin(options: RegExpFeatureOptions): PluginObject;
function declare(factory: (api: BabelAPI) => PluginObject): BabelPlugin;
function REQUIRED_VERSION(version: number): number | string;
interface BabelAPI {
assertVersion(version: number | string): void;
types: typeof t;
template: typeof template;
traverse: typeof traverse;
}
interface RegExpFeatureOptions {
name: string;
feature: string;
}
interface PluginObject {
name: string;
visitor: Visitor;
}
type BabelPlugin = (api: BabelAPI) => PluginObject;
type Visitor = Record<string, Function>;The plugin:
createRegExpFeaturePlugin from @babel/helper-create-regexp-features-plugindeclare from @babel/helper-plugin-utils to create the plugin factory"transform-dotall-regex" and feature "dotAllFlag"REQUIRED_VERSION(7) assertions flagThe plugin performs the following transformations:
s flag (e.g., /pattern/s)g, i, m, u. to [^] when dotAll flag is presents flag from the transformed regex// Core plugin types
type BabelPlugin = (api: BabelAPI) => PluginObject;
// Babel API provided to plugin factory
interface BabelAPI {
assertVersion(version: number | string): void;
types: typeof t; // @babel/types
template: typeof template; // @babel/template
traverse: typeof traverse; // @babel/traverse
targets(): Targets; // Browser/Node targets
}
// Plugin configuration options for createRegExpFeaturePlugin
interface RegExpFeatureOptions {
name: string; // Plugin identification name
feature: string; // RegExp feature to transform ("dotAllFlag")
}
// Standard Babel plugin object structure
interface PluginObject {
name: string;
visitor: Visitor; // AST visitor pattern
}
// Version assertion function (global in Babel build)
function REQUIRED_VERSION(version: number): number | string;
function REQUIRED_VERSION(version: string): string;@babel/helper-create-regexp-features-plugin: Core regex transformation functionality@babel/helper-plugin-utils: Babel plugin utilities@babel/core: ^7.0.0-0 (Required Babel version)The plugin leverages Babel's built-in error handling and the regexp features plugin infrastructure. Common scenarios:
REQUIRED_VERSION(7) - throws error if version < 7@babel/helper-create-regexp-features-plugin or @babel/helper-plugin-utils are not availableThis plugin enables dotAll regex functionality in:
The transformed output ([^] character class) is supported in all JavaScript engines that support regular expressions.