【发布时间】:2011-09-16 13:11:28
【问题描述】:
我有很多格式错误的 HTML,例如我正在尝试使用 Lua 来修复它们
<p class='heading'>my useful information</p>
<p class='body'>lots more text</p>
我想用
替换<h2>my useful information</h2>
<p class='body'>lots more text</p>
我尝试使用的是以下 Lua 函数,它传递了整个 html 页面。 但是我有两个问题,我希望 gsub 将替换函数传递给整个匹配项,包括顶部和尾部,然后我将替换顶部和尾部并返回字符串。另一个问题是我的内部替换功能看不到顶部和尾部字段。
对不起,如果这是一个明显的问题,但我仍在学习 Lua。
function topandtailreplace(str,top,tail,newtop,newtail)
local strsearch = top..'(.*)'..tail
function replace(str)
str = string.gsub(str,top,newtop)
str = string.gsub(str,tail,newtail)
return str
end
local newstr = str:gsub(strsearch,replace())
return newstr
end
【问题讨论】:
标签: string lua design-patterns