Bottom line: Use built-in functionality.
Unfortunately, the built-in str:replace is non-conformant in multiple ways, so that will not be a satisfactory bottom line for everyone.