npm.io
1.0.1 • Published 6 years ago

retemplate

Licence
Apache-2.0
Version
1.0.1
Deps
2
Size
55 kB
Vulns
0
Weekly
0

retemplate

Regular Expressions using Tagged Templates

Installation

npm i retemplate

Usage

import { R } from "retemplate";

// composing regular expressions with tagged templates:
const unsafe = "hello?";
const part = R`(?<part>${unsafe})`;
const whole = R`say ${part}`;
const re = new RegExp(whole); // re = /say (?:(?<part>hello\?))/
re.test("say hello?"); // true

// composite matchers
const results = R.matchAll({
    [R`foo`]: 0,
    [R`bar(\d)`]: (_, cap) => parseInt(cap),
    [R`baz`]: R.BREAK,
    [R`quxx`]: 3
}, "foo bar1 bar2 baz quxx");

result; // [0, 1, 2]