npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@opencounter/babel-plugin-transform-modern-regexp

v0.1.0

Published

Enables modern RegExp features in JavaScript

Downloads

70

Readme

babel-plugin-transform-modern-regexp

Build Status npm version

Enables modern RegExp features in JavaScript.

You can get an overview of the plugin in this article.

Table of Contents

Features

The plugin enables the following features for JS regular expressions:

  • "dotAll" s-flag (stage 3 proposal)
  • Named capturing groups (stage 3 proposal)
  • Extended x-flag (non-standard)

See also examples in compat-transpile, and regexp extensions sections of regexp-tree.

dotAll s-flag

See details in the proposal.

By default the . symbol matches all symbols but new lines. The "dotAll" s flag enables matching \n and other new line symbols with the . symbol:

// Simple.
/./s;

// With unicode `u` flag.
/./su;

It is translated into:

// Simple.
/[\0-\uFFFF]/;

// With unicode `u` flag.
/[\0-\u{10FFFF}]/u;

Named capturing groups

See details in the proposal.

Capturing groups in JS regexes until recent supported only numbered-matching.

For example, given /(\d{4})-(\d{2})-(\d{2})/ that matches a date, one cannot be sure which group corresponds to the month and which one is the day without examining the surrounding code. Also, if one wants to swap the order of the month and the day, the group references should also be updated.

Named capture groups provide a nice solution for these issues.

/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/

To backreference a named group, we can use \k<name> notation:

/(?<value>a)\k<value>\1/

The above regexp is translated into:

/(a)\1\1/

Extended x-flag

Note: x-flag is not yet standardized by ES spec. It's a standard flag in PCRE, Python, and other regexes.

Some features, like x-flag currently can only be used via new RegExp(...) pattern, since are not supported yet by JavaScript parsers for regexp literals:

new RegExp(`

  # A regular expression for date.

  (?<year>\\d{4})-    # year part of a date
  (?<month>\\d{2})-   # month part of a date
  (?<day>\\d{2})      # day part of a date

`, 'x');

Translated into:

/(\d{4})-(\d{2})-(\d{2})/;

Plugin options

The plugin supports the following options.

features option

This options allows choosing which specific transformations to apply. Available features are:

  • dotAll
  • namedCapturingGroups
  • xFlag

which can be specified as an extra object for the plugin:

{
  "plugins": [
    ["transform-modern-regexp", {
      "features": [
        "namedCapturingGroups",
        "xFlag"
      ]
    }]
  ]
}

NOTE: if omitted, all features are used by default.

useRe option

This option enables a convenient re shorthand, which allows using multiline regexes with single escape for meta-characters (just like in regular expression literals).

Taking example of the date regexep using standard RegExp constructor:

new RegExp(`

  # A regular expression for date.

  (?<year>\\d{4})-    # year part of a date
  (?<month>\\d{2})-   # month part of a date
  (?<day>\\d{2})      # day part of a date

`, 'x');

we see inconvenient double-escaping of \\d (and similarly for other meta-characters). The re shorthand allows using single escaping:

re`/

  # A regular expression for date.

  (?<year>\d{4})-    # year part of a date
  (?<month>\d{2})-   # month part of a date
  (?<day>\d{2})      # day part of a date

/x`;

As we can see, re accepts a regexp in the literal notation, which unifies the usage format.

In both cases it's translated to simple regexp literal, so no any runtime overhead:

/(\d{4})-(\d{2})-(\d{2})/

NOTE: it supports only template string literals, you can't use expressions there. Be careful also with /${4}/ -- this is treated as a template literal expression, and should be written as /\${4}/ instead.

NOTE: \\1 backreferences should still be escaped with double slashes. This is due template literal strings do not allow \1 treating them as Octal numbers.

useRuntime option

NOTE: the useRuntime option is not implemented yet. Track issue #3 for details.

NOTE: useRuntime is not required: if e.g. named groups are used mostly for readability, the useRuntime can be omitted. If you need to access actual group names on the matched results, the runtime support should be used.

This option enables usage of a supporting runtime for the transformed regexes. The RegExpTree class is a thin wrapper on top of a native regexp, and has identical API.

NOTE: regexp-tree-runtime should be in your dependencies list.

E.g. the date expression from above is translated into:

const RegExpTree = require('regexp-tree-runtime');

...

const re = new RegExpTree(/(\d{4})-(\d{2})-(\d{2})/, {
  flags: 'x',
  source: <original-source>,
  groups: {
    year: 1,
    month: 2,
    day: 3,
  },
});

const result = re.exec('2017-04-17');

// Can access `result.groups`:

console.log(result.groups.year); // 2017

Usage

Via .babelrc

.babelrc

{
  "plugins": ["transform-modern-regexp"]
}

Via CLI

$ babel --plugins transform-modern-regexp script.js

Via Node.js API

require('@babel/core').transform(code, {
  plugins: ['transform-modern-regexp']
});