@babel/plugin-transform-named-capturing-groups-regex

NOTE: This plugin is included in @babel/preset-env, in ES2018 NOTE: This plugin generates code that needs ES6 regular expressions functionalities. If you need to support older browsers, use either the runtime: false option or import a proper polyfill (e.g. core-js).

Examples

In

  1. var re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
  2. console.log(re.exec("1999-02-29").groups.year);

Out

  1. var re = _wrapRegExp(/(\d{4})-(\d{2})-(\d{2})/, { year: 1, month: 2, day: 3 });
  2. console.log(re.exec("1999-02-29").groups.year);

Installation

  1. npm install --save-dev @babel/plugin-transform-named-capturing-groups-regex

Usage

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

Via CLI

  1. babel --plugins @babel/plugin-transform-named-capturing-groups-regex script.js

Via Node API

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

Options

runtime

boolean, defaults to true

When this option is disabled, Babel doesn’t wrap RegExps with the _wrapRegExp helper. The output only supports internal group references, and not runtime properties:

  1. var stringRe = /(?<quote>"|').*?\k<quote>/;
  2. stringRe.test("'foo'"); // "true", works
  3. stringRe.exec("'foo'").groups.quote; // Error

You can read more about configuring plugin options here