Skip to content
Siddharth edited this page May 21, 2021 · 2 revisions

betteregex

Welcome to the betteregex wiki! This page contains the documentation for this package

Installation

Install using npm:

$ npm i betteregex

Documentation

The betteregex module exposes a regex function, which can be used as a tagged template literal, like so:

const {regex} = require("betteregex");
regex`
    ...
`

You can add JavaScript-style comments, and all whitespace will be ignored. Also note that you don't have to escape backslashes if you are escaping something else (\d+ instead of \\d+)

regex`
    // So this is a regex
    /*
      I'm gonna search for a few numbers
    */
    \d+
    // Maybe a word or two
    \w{2}
    // In case you need a newline
    \n
`

You can interpolate as usual. If you interpolate a regex, the source of the regex will be interpolated instead

const someRegex = /abc.*/g

regex`
    ${someRegex}
    \d\s+ /* Normal stuff */
`

But if there is an interpolation at the very end, it will be used as the flags

regex`
    // Some regex...
${"gi"}`
Clone this wiki locally