retemplate
v1.0.1
Published
Regular Expressions using tagged templates
Downloads
8
Readme
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]