@babel/plugin-transform-unicode-sets-regex
Questa pagina è stata tradotta da PageTurner AI (beta). Non ufficialmente approvata dal progetto. Hai trovato un errore? Segnala problema →
Questo plugin è incluso in @babel/preset-env, in ES2024
Questo plugin trasforma le espressioni regolari che utilizzano il flag v, introdotto dalla proposta RegExp set notation + properties of strings, in espressioni regolari che usano il flag u.
Trasforma esclusivamente la sintassi /.../v e non modifica il costruttore new RegExp, poiché i suoi argomenti non possono essere pre-trasformati staticamente: per gestire il comportamento runtime di funzioni/classi sarà necessario utilizzare un polyfill.
Esempio
Intersezione
/[\p{ASCII}&&\p{Decimal_Number}]/v;
verrà trasformato in
/[0-9]/u;
Differenza
// Non-ASCII white spaces
/[\p{White_Space}--\p{ASCII}]/v;
verrà trasformato in
/[\x85\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000]/u;
Proprietà delle stringhe
/^\p{Emoji_Keycap_Sequence}$/v.test("*\uFE0F\u20E3");
// true
verrà trasformato in
/^(?:\*️⃣|#️⃣|0️⃣|1️⃣|2️⃣|3️⃣|4️⃣|5️⃣|6️⃣|7️⃣|8️⃣|9️⃣)$/u.test("*\uFE0F\u20E3");
// true
Ecco un elenco delle proprietà supportate. Nota che l'utilizzo delle proprietà delle stringhe con il flag u causerà un errore.
/\p{Emoji_Keycap_Sequence}/u;
// Error: Properties of strings are only supported when using the unicodeSets (v) flag.
Installazione
- npm
- Yarn
- pnpm
- Bun
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
bun add --dev @babel/plugin-transform-unicode-sets-regex
Utilizzo
Con un file di configurazione (Consigliato)
{
"plugins": ["@babel/plugin-transform-unicode-sets-regex"]
}
Tramite CLI
babel --plugins @babel/plugin-transform-unicode-sets-regex script.js
Tramite Node API
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-unicode-sets-regex"],
});