我正在尝试编写一个能够为HTML文本提供资源的函数。问题定义:
function f(txt) return txt:gsub("%s"," ")
现在这适用于以下内容:
f(" hello buddy!") ---> " hello buddy!"
但是根据HTML规范,只有当有两个或更多空格时,额外的空格才需要用
替换。因此,不需要替换单个空间。如果有更多,则不会转换一个空格,但其余空格将转换为
。换句话说,我需要一个功能:
f(" hello buddy!") ---> " hello buddy!"
f(" ") ---> "  "
f(" ") ---> " "
f("hello buddy!") ---> "hello buddy!"
知道我怎么写f()?
答案 0 :(得分:2)
你可能会尝试像
这样的东西txt:gsub("( +)", function(c) return " "..(" "):rep(#c-1) end)
答案 1 :(得分:2)
(有关Alex的回答的注释。发布在这里,所以我可以包含格式化的代码。)
前4个gsub调用可以用单个调用替换,它将查找表作为第二个参数。这比通过代码进行4次传递要快得多。
function sanitize(txt)
local replacements = {
['&' ] = '&',
['<' ] = '<',
['>' ] = '>',
['\n'] = '<br/>'
}
return txt
:gsub('[&<>\n]', replacements)
:gsub(' +', function(s) return ' '..(' '):rep(#s-1) end)
end
答案 2 :(得分:0)
感谢jpjacobs的提示使用函数,这里是完整的函数代码加上一个例子:
---This function sanetizes a HTML string so that the following characters will be shown
-- correctly when the output is rendered in a browser:
-- & will be replaced by &
-- < will be replaced by <
-- > will be replaced by >
-- \n will be replaced by <br/>;
-- (more than one space) will be replaced by (as many as required)
-- @param txt the input text which may have HTML formatting characters
-- @return the sanetized HTML code
function sanitize(txt)
txt=txt:gsub("%&","&")
txt=txt:gsub("%<","<")
txt=txt:gsub("%>",">")
txt=txt:gsub("\n","<br/>")
txt=txt:gsub("(% +)", function(c) return " "..(" "):rep(#c-1) end)
return txt
end
text=[[ <html> hello &bye </html> ]]
print("Text='"..text.."'")
print("sanetize='"..sanitize(text).."'")
输出:
Text=' <html> hello &bye </html> '
sanetize=' <html> hello &bye </html> '