putting '%' in string.gsub() in Lua

gsub, lua, lua-patterns

Solution

Try

print (string.gsub( strs , " ","%%20"))

`%` is used in lua in regex operations eg. `%a` is for all letters Tutorial here

So as to escape it we need to use `%%` to tell that we are actually looking for a percentage sign and not a regex.

Problem

i have this code ``` local strs = "my dog" print (string.gsub( strs , " ","%20")) ``` i just wanted the output will be like this `my%20dog` but i got this error ``` Runtime error ... invalid capture index stack traceback: [C]: ? [C]: in function 'gsub' ``` i have read that `%` is escape in Lua. my question is, how can i apply `%` for my replaced new string (`strs`)?

Original source