使用 Lua 检测字符串中的字符是否存在

我有两种类型的字符串:nxs_flo_dev.nexusfpdesk。 我想测试字符串中是否存在 如果有,我就把字符串分割,否则我什么也不做。

if(ngx.var.host.contains('.') then
    content_by_lua 'ngx.say(ngx.var.host:match("(.-)%."))';
end

是否有一个函数来完成这个任务?因为.contains()不起作用。

点赞
用户107090
用户107090

再次使用match。记得用%转义点:

if ngx.var.host:match('%.') then

如果你想在content_by_lua中执行这个操作,可以这样做:

content_by_lua 'if ngx.var.host:match('%.') then ngx.say(ngx.var.host:match("(.-)%.")) end';

鉴于你的编辑,这是最简单的解决方案:

content_by_lua 'ngx.say(ngx.var.host:match("(.-)%.") or ngx.var.host)';
2018-02-07 09:05:48