Closed rarkins closed 2 years ago
Answering myself, in case it helps someone in future. The answer is to use a replace function:
const RE2 = require('re2');
const oldString = 'foo bar';
const newValue = '999';
const replaceFunction = (_, text, space) => `${text}${space}${newValue}`;
const matchRe = /^([^\s]+)(\s+).*?$/;
const regexResult = oldString.replace(matchRe, replaceFunction);
const re2Match = new RE2(matchRe);
const re2Result = oldString.replace(re2Match, replaceFunction);
console.log({ regexResult, re2Result });
Produces:
{ regexResult: 'foo 999', re2Result: 'foo 999' }
Assuming that this difference in behavior is not considered a bug in node-re2
, please close.
Given this script:
It prints:
What's the best way to overcome that?