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

rett

v1.3.0

Published

Regular expressions template tag

Downloads

5

Readme

rett

Regular Expressions Template Tag

Features

  • Removes need to use double backslash.
  • Escapes special regex characters.
  • Choosing when to escape special characters.
  • Multiline regex with comments and indents.
  • Utility for making complex regex.

Usage

Basic Use

The re function can either take a template tag or be called normally.
When called normally, it takes regex flags and can only take a template tag from then on.

const re = require('rett');

console.log(re`\d+\b`);
// => /\d+\b/

console.log(re('gi')`\b[a-z]\b`);
// => /\b[a-z]\b/gi

Substitutions

Interpolate values just like any other template string.
All values will automatically be escaped.

const text = '?+{[()';

console.log(re`${text} \d+`);
// => /\?\+\{\[\(\) \d+/

Multiline Regex

The re.line function is used for multiline regex.
It has support for indents as well as single-line comments.
It works in the same way as the re function.

console.log(re.line`
    (
        \d+     // Digits
        [A-Z]   // Letter
    )
`);

// => /(\d+[A-Z])/

Ignoring Escape

Ignore escapes if you are planning on building regex bit by bit.
This is done with the re.ignore function which can take either a string or a template tag.
The re.raw function is also available for disabling escaping altogether.

const special = '{0,2}';

console.log(re`1${re.ignore(special)}`);
// => /1{0,2}/

console.log(re.raw`1${special}`);
// => /1{0,2}/

const digits = re.ignore`\d+`;

console.log(re`-?${digits}`);
// => /-?\d+/

Utility

re.escape()

Escapes special regex characters from a string.
Can take a template tag and will escape interpolated values.

console.log(re.escape('What?'));
// => 'What\\?'

re.unescape()

Unescapes special regex characters from a string.
Can take a template tag and will unescape interpolated values.
Note that this is not a perfect reversal.

console.log(re.unescape('What\\?'));
// => 'What?'

re.join()

Joins an array together by a character.
Escapes the values of the array.
Useful for making different possibilties with | in regex.

const list = re.join(['(', ')', '?']);
const regex = re`${list}`;

console.log(regex);
// => /\(|\)|\?/

Options

re.options

You can set the default options here.

  • escape: default for escaping substitutions.
  • multiline: default for parsing as multiline.
  • debug: logs to console whenever a regex is made.
  • flags.default: default for flags if no flags provided.
  • flags.addtion: flags to add onto exisiting flags.
re.options = {
    escape: true,
    multiline: true,
    debug: true,
    flags: {
        default: 'u',
        addition: 'u'
    }
};

console.log(re`
    ABC  // Matches A, B, then C.
    (\d) // Then a number.
`);

// => /ABC(\d)/u

re.specialEscapeRegex

Regex for escaping special characters.
Reassign with your own if needed.

re.RegExp

Constructor used for creating new regular expression instances.
Reassign with your own if needed.

Other

re.create()

Creates a new regex from the values inputted.
Used internally.

console.log(re.create(['a', 'b'], [1], { flags: 'g' }));
// => /a1b/g

re.isTemplateTag()

Checks if a value is a template tag.
Used internally.

console.log(re.isTemplateTag`asdf`);
// => true