4

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)?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
gadss
  • 21,687
  • 41
  • 104
  • 154

1 Answers1

10

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.

Rishabh
  • 1,185
  • 1
  • 12
  • 28
  • 3
    please note: Lua does NOT support regex, just patterns (which are by far less powerful but have a more lightweight implementation than regexes) – Henrik Ilgen Jun 14 '12 at 06:22