uhop / node-re2

node.js bindings for RE2: fast, safe alternative to backtracking regular expression engines.
Other
489 stars 53 forks source link

Replacement function receives unmatched capture groups as empty string instead of undefined #145

Closed dset closed 1 year ago

dset commented 1 year ago

There seems to be a mismatch in how unmatched capture groups are treated in the replace function. RE2 sends them to the replacement function as empty string, which makes it hard to distinguish between unmatched pattern and empty capture. Related https://github.com/uhop/node-re2/pull/8.

// Version 1.17.8
var RE2 = require("re2");

const regex = /hello (world)?/;
const regexRE2 = new RE2(regex);

const string = "hello ";

string.replace(regex, (match, capture) => {
  console.log(`'${match}'`, `'${capture}'`);
});

string.replace(regexRE2, (match, capture) => {
  console.log(`'${match}'`, `'${capture}'`);
});

regexRE2.replace(string, (match, capture) => {
  console.log(`'${match}'`, `'${capture}'`);
});

// Output:
// 'hello ' 'undefined'
// 'hello ' ''
// 'hello ' ''
uhop commented 1 year ago

It could be chalked off as a difference between RE2 and RegExp. Example: https://github.com/uhop/node-re2#mismatched-behavior

I'll try what I can but I don't hold out much hope.