uhop / node-re2

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

Overcoming capture group differences with JS regex #115

Closed rarkins closed 2 years ago

rarkins commented 2 years ago

Given this script:

const RE2 = require('re2');

const oldString = 'foo bar';
const newValue = '999';
const matchRe = /^([^\s]+)(\s+).*?$/;
const replaceRe = `$1$2${newValue}`;
const regexResult = oldString.replace(matchRe, replaceRe);
const re2Match = new RE2(matchRe);
const re2Result = oldString.replace(re2Match, replaceRe);
console.log({ regexResult, re2Result });

It prints:

{ regexResult: 'foo 123', re2Result: 'foo$2999' }

What's the best way to overcome that?

rarkins commented 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.