github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/stdlib/string/testdata/gsub.lua (about) 1 assert(string.gsub("xxx", "x", "y", 2) == "yyx") 2 3 assert(string.gsub("xyz", "%w", "%1%0") == "xxyyzz") 4 assert(string.gsub("xyz", "", "a") == "axayaza") 5 assert(string.gsub("xyz", "()", "%1") == "1x2y3z4") 6 7 assert(string.gsub("hello world", "(%w+)", "%1 %1") == "hello hello world world") 8 assert(string.gsub("hello world", "%w+", "%0 %0", 1) == "hello hello world") 9 assert(string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") == "world hello Lua from") 10 x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) 11 return load(s)() 12 end) 13 assert(x == "4+5 = 9") 14 15 local t = {name="lua", version="5.3"} 16 x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) 17 assert(x == "lua-5.3.tar.gz") 18 19 s, n = string.gsub("x x x x", " ", "y") 20 assert(n == 4 and s == "xyxyyxyx") 21 22 s, n = string.gsub("xyx", "x", "%%%1") 23 assert(n == 2 and s == "%xy%x")