Function between

  • Takes three parsers, open, close, and parser. between matches multiple instances of parser, surrounded by open and close. It returns the result of parser.

    This is a look-ahead combinator. It keeps trying the close parser until it succeeds. That means you can use it like this, and parser won't consume the ending quote:

    const quotedString = between(quote, quote, anyChar);
    

    This combinator succeeds even if it parses zero instances of parser. So for the above parser, this input would succeed: "".

    If you want it to consume at least one instance of parser, use between1.

    Even though this is a look-ahead combinator, it won't work if you supply a greedy parser. You can make the above parser fail simply by adding many1:

    const quotedString = between(quote, quote, many1(anyChar));
    

    many1(anyChar) will consume all input until the end of the string, not giving the close parser a chance to succeed.

    This combinator is obviously expensive, as it applies the close parser at every step. This combinator can also get into an infinite loop if parser succeeds without consuming any input.

    Type Parameters

    • O
    • C
    • P

    Parameters

    • open: Parser<O>

      parser for the opening delimiter

    • close: Parser<C>

      parser for the closing delimiter

    • parser: Parser<P>

      parser for the content

    Returns Parser<P[]>

    the result of parser.

Generated using TypeDoc