【发布时间】:2021-07-20 08:25:43
【问题描述】:
我想用某个函数替换匹配的字符串。
我已使用 '%1' 查找字符串,但无法使用匹配的字符串。
print(text) 显示 %1,不匹配的字符串。
original_text = "Replace ${test01} and ${test02}"
function replace_function(text)
-- Matched texts are "test01" and "test02"
-- But 'text' was "%1", not "test01" and "test02"
local result_text = ""
if(text == "test01") then
result_text = "a"
elseif(text == "test02") then
result_text = "b"
end
return result_text
end
replaced_text = original_text:gsub("${(.-)}", replace_function("%1"))
-- Replace result was "Replace and"
-- But I want to replace "Replace ${test01} and ${test02}" to "Replace a and b"
print(replaced_text)
如何在 gsub 中使用匹配的字符串?
【问题讨论】:
标签: lua