What is @babel/plugin-proposal-unicode-property-regex?
The @babel/plugin-proposal-unicode-property-regex package allows Babel to compile regular expressions using Unicode property escapes (`\p{...}` and `\P{...}`) to ES5. This is particularly useful for writing more readable and maintainable regular expressions that match characters based on their Unicode properties, such as letters, numbers, script, or emoji.
What are @babel/plugin-proposal-unicode-property-regex's main functionalities?
Unicode property escapes in regular expressions
This feature enables the use of Unicode property escapes in regular expressions to match characters based on their Unicode properties. In this example, the regular expression matches one or more characters that are part of the Hiragana script.
/\p{Script=Hiragana}+/u.source
Other packages similar to @babel/plugin-proposal-unicode-property-regex
regexpu-core
regexpu-core is a package that transforms ES2015 Unicode regular expressions into equivalent ES5. It provides similar functionality to @babel/plugin-proposal-unicode-property-regex by allowing developers to use Unicode property escapes in their regular expressions. However, regexpu-core is a more focused library that specifically targets the transformation of Unicode regular expressions, while @babel/plugin-proposal-unicode-property-regex is a Babel plugin that integrates with the Babel ecosystem for broader JavaScript code transformation.
unicode-match-property-ecmascript
unicode-match-property-ecmascript is a lower-level package that provides the functionality to match Unicode properties and values in accordance with the ECMAScript specification. It is used by other packages, including regexpu-core, to implement Unicode property escapes in regular expressions. Unlike @babel/plugin-proposal-unicode-property-regex, which is a Babel plugin for transforming code, unicode-match-property-ecmascript serves as a utility library for dealing with Unicode properties, making it a complementary rather than a directly similar package.
@babel/plugin-proposal-unicode-property-regex
Compile Unicode property escapes (\p{…}
and \P{…}
) in Unicode regular expressions to ES5 or ES6 that works in today’s environments.
Note: the Unicode property escape syntax is non-standard and may or may not reflect what eventually gets specified.
Here’s an online demo.
Installation
npm install @babel/plugin-proposal-unicode-property-regex
Usage
Via .babelrc
(recommended)
.babelrc
{
"plugins": ["@babel/plugin-proposal-unicode-property-regex"]
}
Via CLI
babel --plugins @babel/@babel/plugin-proposal-unicode-property-regex script.js
Via Node.js API
require("@babel/core").transform(code, {
"plugins": ["@babel/plugin-proposal-unicode-property-regex"]
});
To transpile to ES6/ES2015:
require("@babel/core").transform(code, {
"plugins": [
["@babel/plugin-proposal-unicode-property-regex", { "useUnicodeFlag": false }]
]
});
Options
useUnicodeFlag
(defaults to true
)
When disabled with false
, the transform converts Unicode regexes to
non-Unicode regexes for wider support, removing the u
flag. See https://github.com/mathiasbynens/regexpu-core#useunicodeflag-default-false for more information.
Author