Skip to main content

@babel/plugin-transform-unicode-sets-regex

info

This plugin is included in @babel/preset-env, in ES2024

This plugin transforms regular expressions using the v flag, introduced by the RegExp set notation + properties of strings proposal, to regular expressions that use the u flag.

It only transforms /.../v syntax and it does not patch the new RegExp constructor, since its arguments cannot be pre-transformed statically: to handle runtime behavior of functions/classes, you will need to use a polyfill instead.

Example

Intersection

input.js
/[\p{ASCII}&&\p{Decimal_Number}]/v;

will be transformed to

output.js
/[0-9]/u;

Difference

input.js
// Non-ASCII white spaces
/[\p{White_Space}--\p{ASCII}]/v;

will be transformed to

output.js
/[\x85\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000]/u;

Property of Strings

input.js
/^\p{Emoji_Keycap_Sequence}$/v.test("*\uFE0F\u20E3");
// true

will be transformed to

output.js
/^(?:\*️⃣|#️⃣|0️⃣|1️⃣|2️⃣|3️⃣|4️⃣|5️⃣|6️⃣|7️⃣|8️⃣|9️⃣)$/u.test("*\uFE0F\u20E3");
// true

Here is a list of supported properties. Note that using property of strings with u-flag will error.

input.js
/\p{Emoji_Keycap_Sequence}/u;
// Error: Properties of strings are only supported when using the unicodeSets (v) flag.

Installation

npm install --save-dev @babel/plugin-transform-unicode-sets-regex

Usage

babel.config.json
{
"plugins": ["@babel/plugin-transform-unicode-sets-regex"]
}

Via CLI

Shell
babel --plugins @babel/plugin-transform-unicode-sets-regex script.js

Via Node API

JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-unicode-sets-regex"],
});