Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rett

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rett

Regular expressions template tag

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 11 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc