Function createEncapsulatingRegex

  • Builds a regex that matches a string between two provided strings. Supports regex as boundaries as well.

    Parameters

    • left: string | RegExp

      string or regex to match before

    • right: string | RegExp
    • Optional flags: string

      regex flags - 'g' and 's' are always added to whatever flags are passed.

    Returns RegExp

    A regular expression that matches text between the left and right patterns.

    Remarks

    The function accepts either strings or regular expressions as the left and right patterns. If a string is provided, it will be escaped to form a valid regular expression. The function also accepts an optional flags parameter to specify regular expression flags.

    Example

    const regex = createEncapsulatingRegex(/a/, /b/)
    'abc'.match(regex)?.groups?.mid // 'c'

Generated using TypeDoc