Vital.sandbox
String

string.gsub

Shared

Replaces occurrences of a pattern with a replacement


Syntax

local result, count = string.gsub(input, pattern, replacement, max_replacements = nil)

Parameters

TypeNameDescription
stringinputString to be converted
stringpatternLua pattern to find
string, table or functionreplacementReplacement string, table, or function
intmax_replacementsMaximum number of replacements to make

Returns

TypeNameDescription
stringresultAltered string with replacements
intcountNumber of substitutions that occurred

Examples

Simple text replacement
local result, count = string.gsub("hello world", "world", "Lua")

engine.print("info", result) --'hello Lua'
engine.print("info", count) --1
Replace all occurrences
local result = string.gsub("aaa", "a", "b")

engine.print("info", result) --'bbb'
Limit the number of replacements
local result, count = string.gsub("aaa", "a", "b", 2)

engine.print("info", result) --'bba'
engine.print("info", count) --2
Replace using a pattern
local result = string.gsub("hello world", "%a+", "word")

engine.print("info", result) --'word word'
Remove all spaces from a string
local result = string.gsub("h e l l o", " ", "")

engine.print("info", result) --'hello'
Replace all digits with a placeholder
local result = string.gsub("call 555-1234", "%d", "X")

engine.print("info", result) --'call XXX-XXXX'
Capture and reuse groups in replacement
local result = string.gsub("hello world", "(%a+)", "<%1>")

engine.print("info", result) --'<hello> <world>'
Swap two words using captures
local result = string.gsub("first second", "(%a+) (%a+)", "%2 %1")

engine.print("info", result) --'second first'
Use a function to transform matches
local result = string.gsub("I have 2 apples and 3 oranges", "%d+", function(n)
    return tonumber(n)*2
end)

engine.print("info", result) --'I have 4 apples and 6 oranges'
Use a table to map replacements
local replacements = {
    hello = "goodbye",
    world = "moon"
}
local result = string.gsub("hello world", "%a+", replacements)

engine.print("info", result) --'goodbye moon'
Capitalize the first letter of each word
local result = string.gsub("hello world from lua", "%a+", function(word)
    return string.upper(string.sub(word, 1, 1))..string.sub(word, 2)
end)

engine.print("info", result) --'Hello World From Lua'
Strip HTML tags from a string
local html = "<p>Hello <b>world</b></p>"
local result = string.gsub(html, "<[^>]+>", "")

engine.print("info", result) --'Hello world'
Escape special characters
local result = string.gsub("cost: $5.00", "%$", "USD ")

engine.print("info", result) --'cost: USD 5.00'
Convert snake_case to camelCase
local result = string.gsub("my_variable_name", "_(%a)", function(letter)
    return string.upper(letter)
end)

engine.print("info", result) --'myVariableName'
Return original string when pattern is not found
local result, count = string.gsub("hello", "xyz", "abc")

engine.print("info", result) --'hello'
engine.print("info", count) --0

On this page