Function matchGroups

  • Returns an array in which the first element is the match and remaining elements are the capturing groups. Returns undefined if the string has no match for regex. (This is different from the built-in String.prototype.match(), which returns null if there are no matches, but more consistent with the conventions in the Flurp library.)

    Remarks

    Returned array contains additional properties with string indices as per the underlying String.prototype.match() implementation.

    Example

    import * as S from "flurp/string";

    const xyDigits = S.matchGroups(/(\d),(\d)/);
    xyDigits("(4,6)"); // [ '4,6', '4', '6', index: 1, input: '(4,6)', groups: undefined ]
    xyDigits("weasel"); // undefined

    const oopsHasGlobal = S.matchGroups(/(\d),(\d)/g);
    oopsHasGlobal("(4,6)"); // null

    Parameters

    • regex: RegExp

      global flag must not be set, or will return null

    Returns ((s: string) => undefined | RegExpMatchArray) | ((_: string) => null)