Functions will be invoked with the matched text, and their result will be coerced to a string and used as the new replacement text. PEG replacement functions will also receive any captures as additional arguments after the full matching text.
This also allows passing non-function, non-byteviewable values, which will be converted to strings during replacement (only once, and only if at least one match is found).
I standardized on subst instead of repl for this concept
It seems a little silly to allow a function argument to string/replace, but I felt it was nicer if all the replacement functions behaved the same way. And it does mean that you can potentially save yourself work if no match is found. You can also pass an impure function to string/replace-all and return different replacements for each match, which might be useful.
Functions will be invoked with the matched text, and their result will be coerced to a string and used as the new replacement text. PEG replacement functions will also receive any captures as additional arguments after the full matching text.
This also allows passing non-function, non-byteviewable values, which will be converted to strings during replacement (only once, and only if at least one match is found).
subst
instead ofrepl
for this conceptstring/replace
, but I felt it was nicer if all the replacement functions behaved the same way. And it does mean that you can potentially save yourself work if no match is found. You can also pass an impure function tostring/replace-all
and return different replacements for each match, which might be useful.