Skip to main content

optional

The given parser may or may not match. This combinator can not fail.

info

Available since: v1.0.0-alpha.4

Type declaration

const optional: (parser: Parser) => Parser;

Example

In this example we are going to parse the string "Hello" which may not be present.

import { optional, string } from "rudus";

const parser = optional(string("Hello"));

const result = parser.run("World");

The result of the parser above will be:

{
"input": "World",
"isError": false,
"offset": 0,
"result": null
}