Ok I figured it out. It was kind of dumb. I am creating the JS in a string in Perl, and I neglected to escape the \ in the regex. It should have been:
oldname.replace(/\\d+/, index);
Simply because it was in a heredoc.
solved jQuery-UI Sortable and string replace issue [closed]