@babel/plugin-syntax-unicode-sets-regex is a Babel syntax plugin that enables parsing of regular expressions using the unicodeSets (v) flag. This flag provides enhanced Unicode property matching capabilities in JavaScript regular expressions, including intersection, subtraction, and string literals within character classes.
npm install --save-dev @babel/plugin-syntax-unicode-sets-regex// Default import (most common)
import plugin from "@babel/plugin-syntax-unicode-sets-regex";For CommonJS:
const plugin = require("@babel/plugin-syntax-unicode-sets-regex");This plugin is typically configured in your Babel configuration file to enable parsing of unicode sets regex syntax:
// babel.config.js
module.exports = {
plugins: [
"@babel/plugin-syntax-unicode-sets-regex"
]
};With this plugin enabled, Babel can parse regular expressions with the v flag:
// These regex patterns become parseable with the plugin
const regex1 = /[[0-7]&&[5-9]]/v; // Intersection
const regex2 = /[[a-z]--[aeiou]]/v; // Subtraction
const regex3 = /[\p{ASCII}--\q{abc|def}]/v; // String literalsThis plugin is a syntax-only plugin that:
v flag in regular expressionsMain plugin factory that creates a Babel plugin for unicodeSets regex syntax parsing.
/**
* Default export function that creates a Babel plugin for parsing unicodeSets regex syntax
* @param api - Babel's plugin API providing version checking and utilities
* @returns Babel plugin object configured for unicodeSets syntax support
*/
export default declare((api: PluginAPI) => PluginObject);
interface PluginAPI {
version: string;
assertVersion(version: number | string): void;
cache: Map<any, any>;
env: (envName?: string) => string;
caller: (callback: (caller: any) => boolean) => boolean;
}
interface PluginObject {
name: string;
manipulateOptions: (opts: any, parserOpts: ParserOptions) => void;
}The plugin function:
api.assertVersion(7) to ensure Babel 7+ compatibilitycreateRegExpFeaturePlugin() from @babel/helper-create-regexp-features-pluginmanipulateOptionsThe plugin integrates with Babel's parser through the manipulateOptions function:
/**
* Babel plugin manipulation options function that adds regexpUnicodeSets parser support
* @param opts - Babel compilation options
* @param parserOpts - Parser-specific options including plugins array
*/
manipulateOptions(opts: any, parserOpts: ParserOptions): void;
interface ParserOptions {
plugins: string[];
sourceType?: "module" | "script";
allowImportExportEverywhere?: boolean;
allowReturnOutsideFunction?: boolean;
}This function adds "regexpUnicodeSets" to parserOpts.plugins array, enabling the parser to recognize and process regex literals with the v flag.
/**
* Babel Plugin API interface providing core plugin development utilities
*/
interface PluginAPI {
/** Babel version string */
version: string;
/** Assert that Babel version meets requirements */
assertVersion(version: number | string): void;
/** Plugin cache for storing data between compilation runs */
cache: Map<any, any>;
/** Get current environment (development, production, test) */
env: (envName?: string) => string;
/** Check properties of the build tool calling Babel */
caller: (callback: (caller: any) => boolean) => boolean;
}
/**
* Babel Plugin Object structure returned by plugin factory functions
*/
interface PluginObject {
/** Plugin name for debugging and error reporting */
name: string;
/** Function to modify parser options before parsing begins */
manipulateOptions: (opts: any, parserOpts: ParserOptions) => void;
}
/**
* Parser options that control how Babel parses source code
*/
interface ParserOptions {
/** Array of parser plugins to enable during parsing */
plugins: string[];
/** Type of source code being parsed */
sourceType?: "module" | "script";
/** Allow import/export statements anywhere in the code */
allowImportExportEverywhere?: boolean;
/** Allow return statements outside of functions */
allowReturnOutsideFunction?: boolean;
}When this plugin is enabled, the following Unicode Sets regex syntax becomes available:
/[[0-7]&&[5-9]]/v // Matches: 5, 6, 7/[[a-z]--[aeiou]]/v // Matches consonants only/[\q{abc|def}]/v // Matches strings "abc" or "def"/[\p{ASCII}--\q{emoji}]/v // ASCII characters excluding emoji stringsThe plugin includes built-in error handling:
This plugin depends on:
createRegExpFeaturePlugin factorydeclare utility for plugin creation