diff --git a/README.md b/README.md index 2774b65..22b8b9a 100644 --- a/README.md +++ b/README.md @@ -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).* diff --git a/lib/replace.cc b/lib/replace.cc index 2a214e9..5eaeb8f 100644 --- a/lib/replace.cc +++ b/lib/replace.cc @@ -142,6 +142,12 @@ inline std::string replace(const char *data, size_t size, const std::vector1)? & (?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'")); } ]);