string.gsub for Transformations
Replace patterns with strings, functions, or table lookups via gsub.
What Is string.gsub?
string.gsub(s, pattern, repl, n) replaces occurrences of pattern in s with repl. Returns the new string and the number of substitutions.
Simple String Replacement
Replace all occurrences of a literal substring.
local s = "hello world world"
local result, count = s:gsub("world", "Lua")
print(result, count) -- hello Lua Lua 2All lessons in this course
- Lua Pattern Syntax
- Captures and string.match
- string.gmatch for Tokenization
- string.gsub for Transformations