Vai al contenuto principale

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

Traduzione Beta Non Ufficiale

Questa pagina è stata tradotta da PageTurner AI (beta). Non ufficialmente approvata dal progetto. Hai trovato un errore? Segnala problema →

informazioni

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

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

verrà trasformato in

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

Differenza

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

verrà trasformato in

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

Proprietà delle stringhe

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

verrà trasformato in

output.js
/^(?:\*️⃣|#️⃣|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.

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

Installazione

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

Utilizzo

Con un file di configurazione (Consigliato)

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

Tramite CLI

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

Tramite Node API

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