Replacement of a substring with another substring in a string
There is no proper, rigorous definition.  Or, rather, you can make multiple definitions.  All are valid.  You could provide a rigorous definition for replacing the first match, or a rigorous definition for replacing all matches.  Mathematics doesn't tell you which definition is &quot;proper&quot; or &quot;best&quot; or &quot;most useful&quot;.
A meta-comment: You might to be expecting more from mathematics than it can bear.  Mathematics doesn't tell you what is &quot;proper&quot; or &quot;right&quot; or &quot;best&quot;.  It gives us a model for a world: if the structure satisfies such-and-such conditions, then it has such-and-such properties.  You'll have to decide what conditions are appropriate, or what properties you want to have.