Re-implement replaceAll method to fix JDK vs Android discrepancy

If a group's capture was empty, then android's Regex seemed to replace
it with the string 'null', which is batshit. So I simply copy pasted
the code from Android sources, and modified just that part.