local start_string = "3, 2, 1, go!"
-- gsub возвращает новую строку, поэтому вызовы можно сцеплять
start_string = start_string
:gsub("1", "one")
:gsub("2", "two")
:gsub("3", "three")
-- start_string = three, two, one, go!
-- Заменяет все вхождения и считает их
local one_string = "1 1 1"
local all_replaced, count =
one_string:gsub("1", "one")
-- all_replaced = one one one
-- count = 3
-- 4-й аргумент ограничивает количество замен
local first_only = one_string:gsub("1", "one", 1)
-- first_only = one 1 1
-- У gsub нет простого режима: 1-й аргумент - шаблон, и "." в нём - любой символ
local price = "1.5"
local as_pattern = price:gsub(".", ",")
-- as_pattern = ,,,
-- Записанная как %., точка - просто точка
local as_text = price:gsub("%.", ",")
-- as_text = 1,5
-- В замене % тоже особый: пишите %%
local percents = one_string:gsub("1", "100%%")
-- percents = 100% 100% 100%
print("start_string = " .. start_string)
print("all_replaced = " .. all_replaced)
print("count = " .. count)
print("first_only = " .. first_only)
print("as_pattern = " .. as_pattern)
print("as_text = " .. as_text)
print("percents = " .. percents)