@babel/plugin-transform-unicode-sets-regex
情報
このプラグインは@babel/preset-env
に含まれており、ES2024にあります。
このプラグインは、RegExp set notation + properties of strings提案によって導入されたv
フラグを使用する正規表現を、u
フラグを使用する正規表現に変換します。
例
共通部分
input.js
/[\p{ASCII}&&\p{Decimal_Number}]/v
以下のように変換されます
output.js
/[0-9]/u
差分
input.js
// Non-ASCII white spaces
/[\p{White_Space}--\p{ASCII}]/v
以下のように変換されます
output.js
/[\x85\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000]/u;
文字列のプロパティ
input.js
/^\p{Emoji_Keycap_Sequence}$/v.test("*\uFE0F\u20E3");
// true
以下のように変換されます
output.js
/^(?:\*️⃣|#️⃣|0️⃣|1️⃣|2️⃣|3️⃣|4️⃣|5️⃣|6️⃣|7️⃣|8️⃣|9️⃣)$/u.test("*\uFE0F\u20E3");
// true
サポートされているプロパティのリストはこちらです。u
フラグで文字列のプロパティを使用するとエラーになることに注意してください。
input.js
/\p{Emoji_Keycap_Sequence}/u
// Error: Properties of strings are only supported when using the unicodeSets (v) flag.
インストール
- npm
- Yarn
- pnpm
npm install --save-dev @babel/plugin-transform-unicode-sets-regex
yarn add --dev @babel/plugin-transform-unicode-sets-regex
pnpm add --save-dev @babel/plugin-transform-unicode-sets-regex
使い方
設定ファイルを使用する(推奨)
babel.config.json
{
"plugins": ["@babel/plugin-transform-unicode-sets-regex"]
}
CLI経由
シェル
babel --plugins @babel/plugin-transform-unicode-sets-regex script.js
Node API経由
JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-unicode-sets-regex"],
});