Skip to main content

string.gsub

Using lua patterns, returns a copy of str with all matches of the pattern replaced by the given replacement string.

The pattern may be a string, a table or a function.

  • If it is a string, then it can contain captures.
  • If it is a table, the match is looked up in the table as a key, and the value (string) is used to replace it, if it exists.
  • If it is a function, then the function is called for each match, with the value of the match as the first argument, the value of the n argument as the second argument, and the position of the match as the third argument. The function must then return the replacement string to be used for the match.
string.gsub(str: string, pattern: string, replacement: string, max: number): string, number

Arguments

NameDescriptionType
strString to search.string
patternPattern to search for.string
replacementReplacement string or function.string
maxMaximum number of replacements.number

Returns string, number