string.gsub has an optional max_replacements parameter, so atm I'm passing 1 to prevent multiple replacements.
Some more tests:
Lua Code:
/script function gsubtest(pat, str) local i=0; local function repl(m) i=i+1; return string.format("(%d:%s)", i, m); end; df("gsub(%q, %q) -> %q", pat, str, string.gsub(pat, str, repl)); end
/script gsubtest("une rune de puissance", "^r*une")
-> "(1:une) rune de puissance"
/script gsubtest("une rune de puissance", "^r*une ")
-> "(1:une )(2:rune )de puissance"
/script gsubtest(" une rune de puissance", "^r*une ")
-> " une rune de puissance"
Warning: Spoiler
Lua Code:
function gsubtest(pat, str)
local i=0
local function repl(m)
i=i+1
return string.format("(%d:%s)", i, m)
end
df("gsub(%q, %q) -> %q", pat, str, string.gsub(pat, str, repl))
end
It appears the anchor actually works at first. The 3rd example string starts with a space, and there's no match, which is correct. But in the 2nd example, after the first replacement (1:une ), the anchor incorrectly matches at "rune", as if the matcher was reset and thought it was at the start of the string.