Looks like logic from java native regex implementation:
The replacement string may contain references to subsequences captured during the previous match: Each occurrence of ${name} or $g will be replaced by the result of evaluating the corresponding group(name) or group(g) respectively. For $g, the first number after the $ is always treated as part of the group reference. Subsequent numbers are incorporated into g if they would form a legal group reference.
re2js result - jb0wo0123 (logically - it is port for re2j)
golang (regex engine is ported re2 NFA):
if use pattern "as is": "$10$20" -> jw123
if fix groups to "${10}${2}0" -> jb0wo0123
it is because :
In the $name form, name is taken to be as long as possible: $1x is equivalent to ${1x}, not ${1}x, and, $10 is equivalent to ${10}, not ${1}0
node-re2 result: j$20w$20123 if using RE2('(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)', 'g') to do replace all
Is this expected behaviour or need in some other way to do "replace all" in node-re2? Thanks
P.S. Native JS implementation:
r = new RegExp('(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)', 'g')
'abcdefghijklmnopqrstuvwxyz123'.replace(r, '$10$20') // => 'jb0wo0123'
Hello. Thanks for your work with this port for node.js.
Found some difference in replacement work and decided to ask here:
regex =
(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)
string =abcdefghijklmnopqrstuvwxyz123
replacement =$10$20
we need to do "replace all":
jb0wo0123
- here test case https://github.com/google/re2j/blob/e3c736d930e4aca62af2c88f4a536e0285ed8c63/javatests/com/google/re2j/MatcherTest.java#L54The replacement string may contain references to subsequences captured during the previous match: Each occurrence of ${name} or $g will be replaced by the result of evaluating the corresponding group(name) or group(g) respectively. For $g, the first number after the $ is always treated as part of the group reference. Subsequent numbers are incorporated into g if they would form a legal group reference.
jb0wo0123
(logically - it is port for re2j)jw123
jb0wo0123
In the $name form, name is taken to be as long as possible: $1x is equivalent to ${1x}, not ${1}x, and, $10 is equivalent to ${10}, not ${1}0
j$20w$20123
if usingRE2('(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)', 'g')
to do replace allIs this expected behaviour or need in some other way to do "replace all" in node-re2? Thanks
P.S. Native JS implementation: