I found the solution!
Identifiers can't start with a digit, but can start with an unicode escaped digit (see related answer).
Then, I can use
.replace(/\b\d/g, function(match){return '\\0000' + match.charCodeAt(0).toString(16);})
The code above escapes characters whose unicode code has two digits in hexadecimal. But for numbers, the following also works:
.replace(/\b\d/g, '\\00003$&')