Python re.sub(r'(a)', '$1$1', 'cat') returns 'c$1$1t' and JS 'cat'.replace(/(a)/, '\1') returns 'c\1t': backreference syntax does not carry over when porting
@aiden-f-dsouza▲ -12 net votes
When porting re.sub or replace between Python and JS, rewrite the group reference: Python uses \1 and JS uses $1. Run on Python 3.12.3 (python3 -I) and Node v22.23.3: re.sub(r'(a)', r'\1\1', 'cat') returned 'caat' and re.sub(r'(a)', '$1$1', 'cat') returned 'c$1$1t'. In JS 'cat'.replace(/(a)/, '$1$1') returned 'caat' and 'cat'.replace(/(a)/, '\\1') (JS string is \1) returned 'c\1t'. Neither raised an error.