A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/slevithan/babel-plugin-transform-regex below:

slevithan/babel-plugin-transform-regex: Babel plugin for Regex+

babel-plugin-transform-regex

This is a Babel plugin that transpiles tagged Regex+ regex templates into native RegExp literals, enabling syntax for modern, readable regex features (atomic groups, subroutines, insignificant whitespace, comments, etc.) without the need for calling regex at runtime. Although Regex+ is already a lightweight and high-performance library, this takes things further by giving you its developer experience benefits without adding any runtime dependencies and without users paying any runtime cost.

Turns this:

const ipv4 = regex`
  ^ \g<byte> (\.\g<byte>){3} $

  (?(DEFINE)
    (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d)
  )
`;

Into this:

const ipv4 = /^(?:2[0-4]\d|25[0-5]|1\d\d|[1-9]?\d)(?:\.(?:2[0-4]\d|25[0-5]|1\d\d|[1-9]?\d)){3}$/v;

The following call formats are all supported:

Interpolation into the expression is supported, so long as the interpolated values are:

Additional details:

The following options are available when running the Babel plugin:

By default, the regex tag implicitly adds flag v (unicodeSets, supported by Node.js 20 and 2023-era browsers) to generated regexes, but it automatically switches to flag u (while applying v's escaping rules) in environments without native v support. This creates an issue for the Babel plugin, because although it will typically be run in environments that support flag v, the transpiled results may need to run for users in old browsers without native v.

There are several ways to address this:

Tip

You can try all of these options in the demo REPL.

Add this plugin and a recent version of Babel (tested with 7.24–7.26) to your project:

npm install --save-dev @babel/core @babel/cli
npm install --save-dev babel-plugin-transform-regex

Run the following command to compile all of your code from the src directory to lib:

./node_modules/.bin/babel src --out-dir lib --plugins=babel-plugin-transform-regex

To make this easier to run, create a config file in the root of your project named babel.config.json, with this content:

{
  "plugins": ["babel-plugin-transform-regex"]
}

If you're using TypeScript, also add plugin @babel/plugin-syntax-typescript. Here's an example of that which additionally sets plugin options:

{
  "plugins": [
    "@babel/plugin-syntax-typescript",
    ["babel-plugin-transform-regex", {
      "removeImport": true,
      "disableUnicodeSets": true,
      "optimize": true
    }]
  ]
}

Then add a script to your package.json to run the build:

"scripts": {
  "build": "babel src --out-dir lib"
}

After that, you can run it via npm run build.

Created by Steven Levithan.

If you want to support this project, I'd love your help by contributing improvements, sharing it with others, or sponsoring ongoing development.

© 2024–present. MIT License.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4