Skip to content

regex

Tries to match a given regex.

Available since: v1.0.0-alpha.1

Type declaration

const regex: (searchString: RegExp, name?: string) => Parser;

Example

In this example we are going to parse the strings "Hello" and "Hallo" using the same parser.

import { regex, sequenceOf, whitespace } from "rudus";
const regexParser = regex(/H[ae]llo/);
const parser = sequenceOf([regexParser(), whitespace(), regexParser()]);
const result = parser.run(`Hello Hallo`);

The result of the parser above will be:

{
"input": "Hello World",
"isError": false,
"offset": 11,
"result": ["Hello", " ", "Hallo"]
}