• Extracts text from between two substrings in a string.

    Example

    Here's a simple text extraction example:

    const text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
    textBetween(text, 'ipsum', 'consectetur');
    // => { ok: true, value: 'dolor sit amet,' }

    Example

    Here are some examples where the delimiters are special chars to escape:

    textBetween('[music=zap]', '[', ']')
    // => { ok: true, value: 'music=zap' }

    textBetween('^music=zap.', '^', '.')
    // => { ok: true, value: 'music=zap' }

    textBetween('music&playlist?kc|zap', '?', '|')
    // => { ok: true, value: 'kc' }

    Example

    Handle an invalid input string:

    textBetween(42, '[', ']')
    // => { ok: false, error: Error('[strings.textBetween] Expected string value as input') }

    Parameters

    • str: string
    • startsWith: string

      string - The starting substring.

    • endsWith: string

      string - The ending substring.

    Returns Result<string, Error>

    A Result object with either the extracted text on success, or an Error object if the provided input is not a string.

Generated using TypeDoc v0.24.7