Have you tried writing this to one single .replace with passing a function as the second argument? might make it more compact.