Search code examples
luaescapinggsublua-patterns

Lua: Escaping literal % inside string.gsub and the capture %2


Basic question but I've been trying to figure out for a while with no luck.

I am processing urls and need to do a simple replacement.

I need to replace spaces with the literal string %20, but I can't seem to escape the % or the %2 which is reported as an invalid capture.

text = string.gsub(text, "%s+", '%%20')

How many % do I have to use inside gsub to escape the % sign and the %2 capture.


Solution

  • Seems to work for me:

    > text="hello world"
    > print(string.gsub(text, "%s+", '%%20'))
    hello%20world   1
    

    You'll need to show some more code and your error message.