{"id":19216,"library":"babel-plugin-transform-regex","title":"babel-plugin-transform-regex","description":"A Babel plugin that transpiles Regex+ template tag (`regex`…``) calls into native RegExp literals at build time. Current stable version is 6.1.0, updated January 2025. Releases are tied to the base Regex+ library. Key differentiators: eliminates runtime dependency, zero runtime cost, supports modern regex features (atomic groups, subroutines, definition groups, Unicode sets). Designed for Node.js and browser bundlers using Babel.","status":"active","version":"6.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/slevithan/babel-plugin-transform-regex","tags":["javascript","regex","babel-plugin"],"install":[{"cmd":"npm install babel-plugin-transform-regex","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-transform-regex","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-transform-regex","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Base regex+ library used for transformation logic","package":"regex","optional":false}],"imports":[{"note":"ESM-only with default export. CommonJS require not supported.","wrong":"const plugin = require('babel-plugin-transform-regex')","symbol":"default","correct":"import plugin from 'babel-plugin-transform-regex'"},{"note":"This is a Babel plugin option, not an exported symbol. Pass via plugin options object.","wrong":"import { removeImport } from 'babel-plugin-transform-regex'","symbol":"removeImport","correct":"// In .babelrc: { \"plugins\": [[\"babel-plugin-transform-regex\", { \"removeImport\": true }]] }"},{"note":"Regex+ exports `regex` as a named export, not default. The Babel plugin transforms these tagged template literals.","wrong":"import regex from 'regex'; const r = regex`^abc$`;","symbol":"regex","correct":"import { regex } from 'regex'; const r = regex`^abc$`;"}],"quickstart":{"code":"// Install: npm install --save-dev babel-plugin-transform-regex regex\n// .babelrc:\n{\n  \"plugins\": [\"babel-plugin-transform-regex\"]\n}\n\n// source.js:\nimport { regex } from 'regex';\nconst ipv4 = regex`\n  ^ \\g<byte> (\\.\\g<byte>){3} $\n  (?(DEFINE)\n    (?<byte> 2[0-4]\\d | 25[0-5] | 1\\d\\d | [1-9]?\\d)\n  )\n`;\n// After Babel transform:\nconst ipv4 = /^(?:2[0-4]\\d|25[0-5]|1\\d\\d|[1-9]?\\d)(?:\\.(?:2[0-4]\\d|25[0-5]|1\\d\\d|[1-9]?\\d)){3}$/v;","lang":"javascript","description":"Demonstrates setting up the Babel plugin and transforming a Regex+ template to a native RegExp literal with flag v."},"warnings":[{"fix":"Ensure target environments support flag v or use option `disableUnicodeSets: true` to fall back to flag u.","message":"v4.3.0 changed the default flag for generated regexes from u to v (unicodeSets).","severity":"breaking","affected_versions":">=4.3.0"},{"fix":"Use `disableUnicodeSets: true` if you need to avoid flag v in the output.","message":"Option `disableUnicodeSets` was added in v4.0.1 to replace the old behavior of always using flag u.","severity":"deprecated","affected_versions":">=4.0.1"},{"fix":"Ensure all interpolated values are inline literals (strings, numbers, regexes) or `pattern` tags. Otherwise, the `regex` call remains at runtime.","message":"The plugin only transforms static `regex` tagged templates without dynamic interpolation. Variables or complex expressions inside the template are left untransformed.","severity":"gotcha","affected_versions":">=4.0.0"},{"fix":"Test output thoroughly when using `optimize`. Consider not using it for complex patterns.","message":"The `optimize` option (experimental) uses regexp-tree optimizer which may produce incorrect results for flag-v-only syntax like nested character classes.","severity":"gotcha","affected_versions":">=4.1.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Install `regex` as a dependency: npm install regex","cause":"Missing runtime dependency `regex` when the plugin is not used or some templates are not transformed.","error":"Cannot find module 'regex'"},{"fix":"Run: npm install --save-dev babel-plugin-transform-regex","cause":"Plugin is not installed as a devDependency.","error":"Error: [BABEL] unknown plugin 'babel-plugin-transform-regex' specified in"},{"fix":"Use Babel's @babel/plugin-transform-unicode-sets-regex or set `disableUnicodeSets: true` in plugin options.","cause":"Target environment does not support flag v (unicodeSets), but plugin outputs regexes with /v.","error":"Uncaught SyntaxError: Invalid regular expression: invalid flag v"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}