@babel/plugin-transform-unicode-regex

unicode-regex - 图1info

This plugin is included in @babel/preset-env

This plugin transforms regular expression literals to support the /u flag. 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

In

JavaScript

  1. var string = "foo💩bar";
  2. var match = string.match(/foo(.)bar/u);

Out

JavaScript

  1. var string = "foo💩bar";
  2. var match = string.match(
  3. /foo((?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]))bar/
  4. );

Installation

  • npm
  • Yarn
  • pnpm
  1. npm install --save-dev @babel/plugin-transform-unicode-regex
  1. yarn add --dev @babel/plugin-transform-unicode-regex
  1. pnpm add --save-dev @babel/plugin-transform-unicode-regex

Usage

babel.config.json

  1. {
  2. "plugins": ["@babel/plugin-transform-unicode-regex"]
  3. }

Via CLI

Shell

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

Via Node API

JavaScript

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