No description of passing a function to string.gsub

#1

The documentation of string.gsub, on the string library page, contains no description of what the function should do that you can pass as the third repl argument. The function takes a capture of the given pattern. If the function returns a string, the value returned is substituted back into the string, like so:

local s = "spoil this, spoil that, spoil everything!"
s = s:gsub("spoil", function(capture)
    return "[spoiler]" .. capture .. "[/spoiler]"
end)
print(s) --> [spoiler]spoil[/spoiler] this, [spoiler]spoil[/spoiler] that, [spoiler]spoil[/spoiler] everything!

Currently, it is only mentioned that you can pass a function, not what it should take/return:

5 Likes

closed #2

This topic was automatically closed after 6 minutes. New replies are no longer allowed.

0 Likes

opened #3
1 Like