Currently (in V6, I didn’t check V7 yet), the replace operation treats an empty string the same as a NULL: one gets the error “Cannot execute replace operations on undefined or empty strings.”
This is annoying, since it goes counter to what the documentation says: “If the pattern does not match, the unchanged string will be returned.” and “All strings that correspond to the regular expression pattern will be replaced with the string replacement.”
Yet, neither of the following works:
local s = “”;
set s = s.replace(“A+”, “B”); // no match case, result should be “”
set s = s.replace(“A*”, “B”); // match case, result should be “B”
It is also mathematically unsound and a behaviour that will surprise programmers in general, as there is, to my knowledge, no other programming language that would not allow pattern matches on empty strings. The more so as there are valid regular expressions that DO match an empty string. Hence, the implemented behaviour is comparable to having an addition operator that would raise an exception if the first operand was 0.
Would it be possible to change the implementation according to the documentation?
I’ll open an internal ticket for the problem, however, I cannot promise when and if it will be changed. Those functions have evolved historically and we first have to assess risks of breaking anything.
Yes, that was my though also. There could be a Service that catches the exception to detect an empty string. However, this would be in reliance on non-documented behaviour.