How to reference the replaced character in string.gsub

string.gsub question, how would I use string.gsub to include the input in the replacement string, example:

Input: 123456789

Output: hi1hi2hi3hi4hi5hi6hi7hi8hi9

Both of these are valid solutions

The %1 pattern? (Or functions)

print(("123456789"):gsub('%d', "hi%1")) --> hi1hi2hi3hi4hi5hi6hi7hi8hi9
1 Like

Use the %1 pattern, which references the character to be replaced:

local s = ("123456789"):gsub("%d", "hi%1")
1 Like