Lua 在字符串每四个字符插入一个值

我有一个需要每四个字符插入一个值的字符串,如下所示。

local string = "24029400001000000000000000000000"
--插入 : 每四个字符

--输出
--2402:9400:0010:0000:0000:0000:0000:0000

我想要的输出是 2402:9400:0010:0000:0000:0000:0000:0000

function string.chunk( str, n )
    local k, t
    t= { }
    for k in str:gmatch( string.rep( ".", n ) ) do
        table.insert( t, k )
    end
    return t
end

x = "24029400001000000000000000000000"
x_new = ""
for k, v in ipairs( x:chunk( 4 ) ) do
  v = v .. ":"
x_new = x_new .. v
end

print(x_new)

--问题在于字符串末尾的冒号
点赞
用户12918181
用户12918181

可能的解决方案:

<script src="https://github.com/fengari-lua/fengari-web/releases/download/v0.1.4/fengari-web.js"></script>
<script type="application/lua">

local s = "24029400001000000000000000000000"
s = s:gsub('....','%1:'):gsub(':$','')
print(s)

</script>
2020-02-29 16:54:00