Skip to content

Commit

Permalink
Merge branch 'le0pard-fix-match-groups-replace'
Browse files Browse the repository at this point in the history
  • Loading branch information
uhop committed Aug 23, 2023
2 parents 8664d4a + e1859e0 commit 0344806
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ console.log('re2_res : ' + re2_res); // prints: re2_res : abc,a,b,c

## Release history

- 1.20.3 *Fix: subsequent numbers are incorporated into group if they would form a legal group reference.*
- 1.20.2 *Fix: added a missing C++ file, which caused a bug on Alpine Linux. Thx, [rbitanga-manticore](https://github.com/rbitanga-manticore).*
- 1.20.1 *Fix: files included in the npm package to build the C++ code.*
- 1.20.0 *Updated RE2. New version uses `abseil-cpp` and required the adaptation work. Thx, [Stefano Rivera](https://github.com/stefanor).*
Expand Down
6 changes: 6 additions & 0 deletions lib/replace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ inline std::string replace(const char *data, size_t size, const std::vector<re2:
result += (std::string)groups[index2];
continue;
}
else if (index && index < groups.size())
{
result += (std::string)groups[index];
result += ch;
continue;
}
result += '$';
result += '0' + index;
result += ch;
Expand Down
42 changes: 42 additions & 0 deletions tests/test_replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,5 +334,47 @@ unit.add(module, [
var re = new RE2(/b(?<a>1)? & (?<b>2)?y/);
var result = re.replace('ab & yz', replacer);
eval(t.TEST("result === 'az'"));
},
function test_replaceGroupSimple(t) {
'use strict';

var re = new RE2(/(2)/);

var result = re.replace('123', '$0');
eval(t.TEST("result === '1$03'"));
result = re.replace('123', '$1');
eval(t.TEST("result === '123'"));
result = re.replace('123', '$2');
eval(t.TEST("result === '1$23'"));

result = re.replace('123', '$00');
eval(t.TEST("result === '1$003'"));
result = re.replace('123', '$01');
eval(t.TEST("result === '123'"));
result = re.replace('123', '$02');
eval(t.TEST("result === '1$023'"));
},
function test_replaceGroupCases(t) {
'use strict';

var re = new RE2(/(test)/g);
var result = re.replace('123', '$1$20');
eval(t.TEST("result === '123'"));

re = new RE2(/(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)/g);
result = re.replace('abcdefghijklmnopqrstuvwxyz123', '$10$20');
eval(t.TEST("result === 'jb0wo0123'"));

re = new RE2(/(.)(.)(.)(.)(.)/g);
result = re.replace('abcdefghijklmnopqrstuvwxyz123', '$10$20');
eval(t.TEST("result === 'a0b0f0g0k0l0p0q0u0v0z123'"));

re = new RE2(/(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)/g);
result = re.replace('abcdefghijklmnopqrstuvwxyz123', '$10$20');
eval(t.TEST("result === 'jtvwxyz123'"));

re = new RE2(/abcd/g);
result = re.replace('abcd123', '$1$2');
eval(t.TEST("result === '$1$2123'"));
}
]);

0 comments on commit 0344806

Please sign in to comment.