Skip to content

--output-match crashes when a group did not take part in the match #684

Closed

Description

@mathiasrw

--output-match (-m) crashes if the pattern has a group that is not part of a match, which is the normal case for alternatives and optional groups.

Seen with rexreplace 7.1.14 on Node v20.20.2.

Reproduce

> echo foobar > my.file
> rexreplace '(f)|(b)' _ my.file -m

Actual

fnode:internal/streams/writable:482
      throw new ERR_INVALID_ARG_TYPE(
      ^

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined

Expected

f
b

Cause

The replacer function in getFinalReplacement() in src/engine.ts (line 242) writes each group to stdout without checking if it is undefined:

for (var i = 1; i < arguments.length - 2; i++) {
	process.stdout.write(arguments[i]);
}

Test

The JS test suite being added on the js-test branch has a test.todo describing the correct behaviour in test/js/output-match.test.js:

BUG: a group that did not take part in the match prints nothing

Activity

  1. mathiasrw commented on Oct 21, 2026

    @mathiasrw
    OwnerAuthor

    Confirmed working since #739

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions