Cache array lengths in string/replace